开发者

WPF app does not execute Application_Exit method when user closes main window

开发者 https://www.devze.com 2023-04-10 01:22 出处:网络
I have a WPF/C# 4.0 App which has an Application file XAML that is: <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

I have a WPF/C# 4.0 App which has an Application file XAML that is:

<Application 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="XXX.App"
Startup="Application_Startup"
Exit="Application_Exit"
>
<Application.Resources>
     
</Application.Resources>

And its exiting method is:

    //it seems that it never passes here. Transferred to MainAppWindow_WindowClosing
    private void Application_Exit(object sender, ExitEventArgs e)
    {
     开发者_如何学运维   this.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
    }

It never passes in this piece of code when the application user closes it. Is it supposed to work this way? Am I doing something wrong?


I have never used it but the docs say that

Occurs just before an application shuts down, and cannot be canceled. An application can shut down for either of the following reasons:

  • The Shutdown method of the Application object is called, either explicitly or as determined by the ShutdownMode property.
  • The user ends the session by logging off or shutting down.

So in your case, these conditions might not be met. Have you tried changing the ShutDownMode?


Application_Exit method will call when you shutdown the application by Application.Current.Shutdown() method or close all windows from your application. (it will call when last window will be closed).

If you call Application.Current.Shutdown() method then it will automatically close all opened windows. Pleaese make sure that your all windows are closed or not ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号