2
votes

I have a problem in a WPF Application. When I close my application from the Window task bar (Close window) or press Alt + F4 it never hits the Application_exit event handler in my App.xaml file.

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

Code behind

private void Application_Exit(object sender, ExitEventArgs e)
{
    Application.Current.Shutdown();
}
2
Doesn't your XAML have a StartupUri attribute ? - Nathan
Why not use this.Close()? It calls Application.Current.Shutdown(). Also, you may need a return statement after your Application.Current.Shutdown(), but I can't remember for sure if that's necessary. - alacy
@AmatuerDev No i am just using Startup in App.xaml file. and it back end Call LOGIN Form. - Khairullah Rehan

2 Answers

3
votes

You have probably set

ShutdownMode="OnExplicitShutdown"

on your App.xaml (or in code behind).

Therefore you need to set a listener on the Closed event of your MainWindow, which is calling the shutdown method.

Or you have set

ShutdownMode="OnLastWindowClose"

Then you probably have some window open, besides the MainWindow.

-1
votes

Your event handler currently shuts down your app:

private void Application_Exit(object sender, ExitEventArgs e)
    {

    Application.Current.Shutdown();

    }

But if you want to do something, your should add it before `Application.Current.Shutdown();' For example:

`private void Application_Exit(object sender, ExitEventArgs e)
        {
        //Do something here, delete unused files etc...
        Application.Current.Shutdown();

        }