WPF Tip: avoid App.Current.Shutdown

In your WPF application’s Exit command handler, be wary of calling App.Current.Shutdown unless you want to bypass the normal ‘Close’ process. It can result in your override of Window.OnClosing to NOT be called, and your closing-event handlers don’t get called either.

If you override the Onclosing method, don’t forget to call the base class OnClosing!

Oh – if your WindowSettings functionality is not working, check for this mistake.

Of course I’ve never done that myself. Oh no. Neeeever.

Discussion:

If you are going to shutdown your application in code other than within the WPF Window or Page class, you can accomplish this by posting an event to the main window which will in turn close itself. If you want code to execute at the close of your application – you can override OnClosing within your Window subclass to accomplish this.

#region OnClosing
/// <summary>
/// This is an override of the event-handler for the Closing event.
/// </summary>
protected override void OnClosingCancelEventArgs e )
{
    UserSettings.SaveWindowPositionIfChangedthis );
    base.OnClosinge );
}
#endregion

This way, you can execute that code within your unit-tests and it will not try to force your running program (which may be your test-runner in this case) to shutdown – because you simply have not defined a handler for that event.

If you do want to call App.Shutdown, then you need to consider the case where it is running within a unit-test and you don’t have an App present to shutdown. In this case App.Current will be null and it would throw an exception where you try to call App.Current.Shutdown. Instead, you could code it thusly:

public void ShutdownApplication()
{
    App.Current?.Shutdown();
}

Here, if App.Current is null it does not make the call to the Shutdown method (as would be the case if this is running within a test-runner instead of the normal application-program).

 

James W. Hurst

1,656 thoughts on “WPF Tip: avoid App.Current.Shutdown”

  1. [url=https://ibenicar.com/]benicar coupon[/url] [url=https://vardenafil911.com/]levetra[/url] [url=https://robaxin365.com/]robaxin 750 mg generic[/url] [url=https://arimidextab.com/]25mg arimidex[/url] [url=https://kamagragen.com/]kamagra 100mg oral jelly walmart[/url] [url=https://buyviagra.us.org/]generic viagra india price[/url] [url=https://tadalafilrm.com/]cialis 60 mg[/url]

  2. [url=https://inderalpro.com/]ordering propanalol[/url] [url=https://baclophen.com/]10 mg baclofen[/url] [url=https://hydroxychloroquineus.com/]generic for plaquenil[/url] [url=https://edtreatmentviag.com/]online pharmacy canada generic viagra[/url] [url=https://flagyltab.com/]flaygle[/url]

  3. In Staffing, anytime, so theoretical are the agents recommended by means of the rate’s best bib situate to buy cialis online forum unrevealed that the location urinalysis of chunk from at tests to seem the notation progressive, forms to give every indication its prevalence. buy clomiphene online Lgdkhu elofbs

  4. I’m no longer sure where you’re getting your info, however great topic.
    I needs to spend some time learning much more or figuring out more.

    Thank you for fantastic information I was in search of this information for my mission.

  5. [url=https://kamagra1000.com/]kamagra oral jelly eu[/url] [url=https://medicinesquick.com/]diovan brand name in india[/url] [url=https://viagramtf.com/]canada pharmacy viagra[/url] [url=https://kamagraorder.com/]cheap kamagra 100mg[/url] [url=https://kamagratbs.com/]kamagra soft tablets online[/url]

  6. [url=https://sildenafilk.com/]can i buy viagra from india[/url] [url=https://advairbuy.com/]advair 115 mcg[/url] [url=https://chloroquinemd.com/]where can i buy aralen[/url] [url=https://viagramtf.com/]sildenafil price comparison in india[/url] [url=https://anafranill.com/]anafranil medicine[/url]

  7. [url=http://prazosine.com/]prazosin 6mg[/url] [url=http://posmeds.com/]cardura online[/url] [url=http://cialispak.com/]where can i buy cheap cialis[/url] [url=http://kamagraorder.com/]buy kamagra uk[/url] [url=http://viagracc.com/]pfizer viagra for sale[/url]

  8. [url=http://buycialislilly.com/]cialis online pharmacy usa[/url] [url=http://painrelieftab.com/]aleve for pain and inflammation[/url] [url=http://viagrabay.com/]viagra over the counter[/url] [url=http://viagraeng.com/]cost of generic viagra in india[/url] [url=http://plaquenil.us.org/]quineprox 200 mg[/url] [url=http://levitranow.com/]how to get levitra from india[/url] [url=http://viagracc.com/]where to order cheap viagra[/url] [url=http://brandgenericmedications.com/]chloramphenicol buy online[/url] [url=http://viagrarmd.com/]viagra generic india[/url] [url=http://kamagra1000.com/]kamagra pills online[/url]

  9. Thanks a bunch for sharing this with all of us you really recognize what you’re talking approximately! Bookmarked. Please additionally consult with my site =). We will have a hyperlink trade contract among us!

  10. An fascinating discussion is price comment. I believe that it is best to write extra on this matter, it may not be a taboo subject however typically persons are not sufficient to speak on such topics. To the next. Cheers