views:

520

answers:

4

Hi All

In the few years I've been using C# (WINFORMS), I've never used WPF. But now I love WPF, but I don't know how the hell I am supposed to exit my application when the user clicks on the Exit menu item from the File menu. Can somone please help me out!?

I DID google this, and guess what? For such a Simple? thing... Nothing related came up.

Thanks

P.S. I have tried:

this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();

... Among many others. Nothing works.

+4  A: 

This should do the trick:

Application.Current.Shutdown();

If you're interested, here's some additional material that I found helpful:

Details on Application.Current

WPF Application LifeCycle

Brian MacKay
+7  A: 

To exit your application you can call

Application.Current.Shutdown();

As described in the documentation to the Application.Shutdown method you can also modify the shutdown behavior of your application by specifying a ShutdownMode:

Shutdown is implicitly called by Windows Presentation Foundation (WPF) in the following situations:

  • When ShutdownMode is set to OnLastWindowClose.
  • When the ShutdownMode is set to OnMainWindowClose.
  • When a user ends a session and the SessionEnding event is either unhandled, or handled without cancellation.

Please also note that Application.Current.Shutdown(); may only be called from the thread that created the Application object, i.e. normally the main thread.

0xA3
Wow! That's ... weird, lol. But thank you :)
lucifer
As I pointed out it's not weird. In WPF Application is a static class. Application.Current is a reference to your currently running Application.
TimothyP
In my opinion, it is a little weird in the sense that this isn't obvious at first glance, and it deviates just enough from past models to throw people off. It makes perfect sense that it works of course.
Brian MacKay
+3  A: 

Hi,

There should not be an Application.ShutDown(); or .Exit() message.

Application is a static class. It does not refer to the current application You need to get to the current application and then shut it down like this:

Application.Current.Shutdown();
TimothyP
+1  A: 

According to my understanding, Application.Current.Shutdown() also has its drawback,

if you want to show a confirm window to let users confirm on quit or not, Application.Current.Shutdown() is irreversible.

I don't understand this. We can get user confirmation before calling `Application.Current.Shutdown()` however.
Veer
I don't see why you should confirm. Too many confirmations is a very bad thing. The fact that somebody took the effort to click to open the File menu, move all the way down to the bottom of the File menu and then click Exit, is pretty much confirmed that they no longer wish to use the application.
lucifer
Veer: In my case, the confirmation window do appear, but even when you choose "cancel" in the confirmation, the main APP exits.
J-T-S:This is better to be designed case by case. Our app, if there are jobs running, the force exit will cause damages, so we better to let them informed in the confirmation dialog