简体   繁体   中英

WPF C# application keeps running for a few seconds after closing. Is this a safe solution?

I have a WPF application with a close button which calls Application.Shutdown() to close the application. The window closes, but the app keeps running. It is clearly visible in Visual Studio that something is going on, but pausing doesn't work anymore.

I have seen similar questions on here which led me to try some other solutions. I have tried all the options for ShutdownMode in App.xaml but none of them seemed to make a difference. Same with Environment.Exit().

The obvious cause would be that a thread is still running but I can't find any such threads.

I finally resorted to a very unelegant method, in App.xaml:

protected override void OnExit(ExitEventArgs e)
{
    base.OnExit(e);
    Process proc = System.Diagnostics.Process.GetCurrentProcess();
    proc.Kill();
}

This actually works, the application close immediately, but I wonder if this might cause any problems somewhere along the line.

Thanks for everyones insights.

I looked into it a little deeper today (with some help, I'll admit), especially because this problem didn't occur in earlier versions and the application has been in development for several years. So tracking back to where the issue first arose gave some insights.

It turns out that there was a MediaElement with an invalid source that caused this problem. Having a non-existing file as source for a MediaElement apparently doesn't give any problem when the application is running, but does cause it to be very slow when closing down. Luckily that is easily fixed.

Digging a little further I noticed that having a storyboard that is still running also might slow down the closing process, but that is just a matter of two or three seconds. The problem with the MediaelEment could cause a lag of (much) more than ten seconds.

So the problem is solved, but my question, though academic now, more or less remains: is it a problem to close down the application by simply killing the process? That still appears to be the quickest way to shut things down.

but my question, though academic now, more or less remains: is it a problem to close down the application by simply killing the process?

It's a bad idea to kill processes. It can be safe in some circumstances but if you do anything that disallows the cleanup of system owned resources you can leave clutter in the form or semaphores and handles. It's good that you kept digging until you found your bug. I would never, ever throw proc.kill into production, especially for your own process.

If you know a child process can be safely killed, maybe. But it would always be better to signal it to shutdown, give it a reasonable amount of time, and if you have to kill it, do it in a way that can't be ignored. Then behave like a dog with a bone until you eliminate the root cause, like you did with this one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM