2

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(); } 
3
  • Doesn't your XAML have a StartupUri attribute ? Commented May 8, 2015 at 8:02
  • 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. Commented May 8, 2015 at 12:05
  • @AmatuerDev No i am just using Startup in App.xaml file. and it back end Call LOGIN Form. Commented May 11, 2015 at 5:43

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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(); } 

2 Comments

Thanks @Ivan Ling for comments. My problem is when i Pressed ALT + F4 keys any where in my Application it close that form but it does't hit This Application_Exit Function. How i possible it when i Pressed Alt+F4 or Close Window from Task Bar it shutdown my Application. Thanks
Ivan Ling's answer doesn't refer to the original question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.