Browse Source
Fix app shutdown cancellation via window (#15438 )
* Don't call dispatcher shutdown, when app shutdown was cancelled
* Assert Dispatcher.UIThread.ShutdownStarted in TryShutdown_Cancellable_By_Preventing_Window_Close
pull/15450/head
Max Katz
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
17 additions and
6 deletions
src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs
tests/Avalonia.Controls.UnitTests/DesktopStyleApplicationLifetimeTests.cs
@ -163,6 +163,7 @@ namespace Avalonia.Controls.ApplicationLifetimes
_ exitCode = exitCode ;
_ isShuttingDown = true ;
var shutdownCancelled = false ;
try
{
@ -180,6 +181,7 @@ namespace Avalonia.Controls.ApplicationLifetimes
if ( ! force & & Windows . Count > 0 )
{
e . Cancel = true ;
shutdownCancelled = true ;
return false ;
}
@ -189,10 +191,14 @@ namespace Avalonia.Controls.ApplicationLifetimes
}
finally
{
_ cts ? . Cancel ( ) ;
_ cts = null ;
_ isShuttingDown = false ;
Dispatcher . UIThread . InvokeShutdown ( ) ;
if ( ! shutdownCancelled )
{
_ cts ? . Cancel ( ) ;
_ cts = null ;
Dispatcher . UIThread . InvokeShutdown ( ) ;
}
}
return true ;
@ -355,9 +355,13 @@ namespace Avalonia.Controls.UnitTests
{
lifetime . SetupCore ( Array . Empty < string > ( ) ) ;
var hasExit = false ;
lifetime . Exit + = ( _ , _ ) = > Assert . Fail ( "lifetime.Exit was called." ) ;
Dispatcher . UIThread . ShutdownStarted + = UiThreadOnShutdownStarted ;
lifetime . Exit + = ( _ , _ ) = > hasExit = true ;
static void UiThreadOnShutdownStarted ( object sender , EventArgs e )
{
Assert . Fail ( "Dispatcher.UIThread.ShutdownStarted was called." ) ;
}
var windowA = new Window ( ) ;
@ -378,7 +382,8 @@ namespace Avalonia.Controls.UnitTests
lifetime . TryShutdown ( ) ;
Assert . Equal ( 1 , raised ) ;
Assert . False ( hasExit ) ;
Dispatcher . UIThread . ShutdownStarted - = UiThreadOnShutdownStarted ;
}
}