简体   繁体   English

WPF C#应用程序在关闭后保持运行几秒钟。 这是安全的解决方案吗?

[英]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. 我有一个带有关闭按钮的WPF应用程序,该按钮调用Application.Shutdown()关闭该应用程序。 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. 在Visual Studio中可以清楚地看到发生了什么事,但是暂停不再起作用了。

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. 我已经尝试了App.xaml中ShutdownMode的所有选项,但似乎没有一个起作用。 Same with Environment.Exit(). 与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: 我终于在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. 事实证明,存在一个MediaElement带有无效源导致了此问题。 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. 在应用程序运行时,使用不存在的文件作为MediaElement的源显然不会带来任何问题,但是在关闭时确实会导致它非常慢。 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. MediaelEment的问题可能导致超过十秒钟的延迟。

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. 我永远也不会将proc.kill投入生产,尤其是对于您自己的过程。

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. 然后表现得像只狗骨头一样,直到消除根本原因为止,就像您对这种骨头所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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