Click here to Skip to main content
15,846,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF application that, when needed, starts a separate update application and then attempts to close itself down using Application.Current.Shutdown(). This used to work fine. Now it doesn't.

I don't know when this problem started because it's a while since I have had cause to exercise this code (yes, I know, slapped hands for me!).

Does anyone have any idea why this function should now appear to do absolutely nothing at all as far as I can tell?

Any ideas would be welcome.

Kind wishes - Patrick

What I have tried:

I have tried looking at the threads in Process Explorer and the main thread seems to have no other threads running, either foreground or background.

Environment.Exit() does kill the application but apparently is a bit 'rude'.
Updated 18-Mar-21 3:30am

The solution turns out to be embarrassingly simple. There was no return statement after the call to Application.Current.Shutdown() so the remainder of the Application.OnStartup() method was executing normally.

The details are here: Why does my C# WPF program keep executing lines after Application.Shutdown()? - Stack Overflow[^]
Share this answer
Are you trying to call the method from a background thread?
This method can be called only from the thread that created the Application object.
Share this answer
Patrick Skelton 18-Mar-21 5:14am    
Hi, Richard! No, it is being called from Application.Startup(), on the main thread.
Richard Deeming 18-Mar-21 5:20am    
Has the Application.Current property been initialized at that point?

If you're in the Startup event, don't you already have a reference to the Application instance?
Richard Deeming 18-Mar-21 5:30am    
The other possibility: Do you have a StartupUri defined? As far as I can see, if that's set, it will always be launched after the Startup event fires.
Patrick Skelton 18-Mar-21 5:45am    
You are right about using 'this' instead of 'Application'. I've modified the code but there is no change in behaviour. StartupUri is null when I call Shutdown() but does get set later (in code that shouldn't be hit but is unless I do Environment.Exit()). Not sure if it means anything, but a breakpoint just after a call to Shutdown() shows this.Dispatcher.HasShutdownStarted to be false.
Richard Deeming 18-Mar-21 6:59am    
The Dispatcher.HasShutdownStarted won't get set until the callback executes, which happens after the Startup event returns, so I'm not surprised it's still false within the event.

I think there must be something else going on in the code. Can you share any of your startup method?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900