@@ -103,7 +103,8 @@ public ApplicationBootstrapper()
103103 if ( ! isShuttingDown )
104104 {
105105 var reason = isSessionEnding ? ApplicationShutdownReason . SessionEnding : ApplicationShutdownReason . ApplicationRequest ;
106- OnShutdownCore ( reason ) ;
106+ // Not a user request: completes synchronously.
107+ _ = OnShutdownCoreAsync ( reason ) ;
107108 }
108109 } ;
109110 }
@@ -237,7 +238,7 @@ void OnBoot()
237238 ExposeRegisteredAppResources ( ) ;
238239
239240 var broker = serviceProvider . TryGetService < IMessageBroker > ( ) ;
240- broker ? . Broadcast ( this , new ApplicationBootCompleted ( ) ) ;
241+ _ = broker ? . BroadcastAsync ( this , new ApplicationBootCompleted ( ) ) ;
241242
242243 bootCompletedHandler ? . Invoke ( serviceProvider ) ;
243244
@@ -378,10 +379,7 @@ public ApplicationBootstrapper OnSingletonApplicationStartup(Action<SingletonApp
378379 void OnBoot ( IServiceProvider serviceProvider )
379380 {
380381 var broker = serviceProvider . GetService < IMessageBroker > ( ) ;
381- broker . Subscribe < ApplicationShutdownRequest > ( this , InvocationModel . Safe , ( s , m ) =>
382- {
383- OnShutdownCore ( ApplicationShutdownReason . UserRequest ) ;
384- } ) ;
382+ broker . Subscribe < ApplicationShutdownRequest > ( this , InvocationModel . Safe , ( s , m ) => OnShutdownCoreAsync ( ApplicationShutdownReason . UserRequest ) ) ;
385383
386384 var args = new SingletonApplicationStartupArgs ( singleton , key ) ;
387385 HandleSingletonApplicationStartup ( args ) ;
@@ -392,7 +390,8 @@ void OnBoot(IServiceProvider serviceProvider)
392390 }
393391 else
394392 {
395- OnShutdownCore ( ApplicationShutdownReason . MultipleInstanceNotAllowed ) ;
393+ // Not a user request: completes synchronously.
394+ _ = OnShutdownCoreAsync ( ApplicationShutdownReason . MultipleInstanceNotAllowed ) ;
396395 }
397396 }
398397
@@ -412,7 +411,7 @@ public void Boot()
412411 /// </summary>
413412 public void Shutdown ( )
414413 {
415- OnShutdownCore ( ApplicationShutdownReason . UserRequest ) ;
414+ _ = OnShutdownCoreAsync ( ApplicationShutdownReason . UserRequest ) ;
416415 }
417416
418417 void OnBootCompleted ( IServiceProvider serviceProvider )
@@ -510,9 +509,12 @@ await Task.Delay(remaining)
510509 }
511510 }
512511
513- void OnShutdownCore ( ApplicationShutdownReason reason )
512+ // Only a user request awaits, letting ApplicationShutdownRequested subscribers
513+ // complete asynchronously; any other reason completes synchronously, as
514+ // required when shutting down from the platform's synchronous events.
515+ async Task OnShutdownCoreAsync ( ApplicationShutdownReason reason )
514516 {
515- var canceled = false ;
517+ var skipShutdown = false ;
516518
517519 try
518520 {
@@ -521,13 +523,20 @@ void OnShutdownCore(ApplicationShutdownReason reason)
521523 var msg = new ApplicationShutdownRequested ( reason ) ;
522524
523525 var broker = serviceProvider . GetService < IMessageBroker > ( ) ;
524- broker . Dispatch ( this , msg ) ;
526+ await broker . DispatchAsync ( this , msg ) ;
527+
528+ if ( isShuttingDown )
529+ {
530+ // the application shut down, e.g. Exit was raised, while awaiting the subscribers
531+ skipShutdown = true ;
532+ return ;
533+ }
525534
526- canceled = msg . Cancel ;
535+ skipShutdown = msg . Cancel ;
527536
528- if ( canceled )
537+ if ( skipShutdown )
529538 {
530- broker . Broadcast ( this , new ApplicationShutdownCanceled ( reason ) ) ;
539+ _ = broker . BroadcastAsync ( this , new ApplicationShutdownCanceled ( reason ) ) ;
531540 return ;
532541 }
533542 }
@@ -536,9 +545,9 @@ void OnShutdownCore(ApplicationShutdownReason reason)
536545
537546 if ( isBootCompleted )
538547 {
539- serviceProvider
548+ _ = serviceProvider
540549 . GetService < IMessageBroker > ( )
541- . Broadcast ( this , new ApplicationShutdown ( reason ) ) ;
550+ . BroadcastAsync ( this , new ApplicationShutdown ( reason ) ) ;
542551
543552 var callbacks = serviceProvider . GetServices < IExpectShutdownCallback > ( ) ;
544553 if ( callbacks != null && callbacks . Any ( ) )
@@ -569,12 +578,12 @@ void OnShutdownCore(ApplicationShutdownReason reason)
569578 }
570579 finally
571580 {
572- if ( ! canceled && reason != ApplicationShutdownReason . ApplicationRequest )
581+ if ( ! skipShutdown && reason != ApplicationShutdownReason . ApplicationRequest )
573582 {
574583 Application . Current . Shutdown ( ) ;
575584 }
576585
577- if ( ! canceled )
586+ if ( ! skipShutdown )
578587 {
579588 serviceProvider = null ;
580589
0 commit comments