为什么我不能用Process.GetCurrentProcess()。杀()退出我的WinForm应用程序?我的、我不、能用、应用程序

2023-09-03 00:21:39 作者:旧時旧夢

现在,当用户想退出我的申请,我做的几件事情我必须(即从服务器断开连接,大大节省了用户的数据...),然后我做到以下几点:

Right now, when the user want to exit my application, I do the few things I have to (ie disconnecting from the server, saving the user data...) and then I do the following :

在使用布尔运算退出所有我mainloops 终止线程仍在运行(通常情况下,我的服务器轮询线程) 请致电Application.Exit();

这需要几秒钟,退出,并没有实际意义(一切都已经保存在服务器上,所以我真的不关心发生了什么事情)

This takes a few seconds to exit, and serves no real purpose (everything is already saved on the server, so I don't really care what happens there)

如果我用这个来代替,我立即终止,没有缺点,我能想到的:

If I use this instead, I got instant termination with no drawback I can think of :

 System.Diagnostics.Process.GetCurrentProcess().Kill();

为什么不我刚刚结束我的过程,并让CLR丢弃的AppDomain?

Why wouldn't I just terminate my process and let the CLR drop the AppDomain ?

我知道,认真处理您的共享资源(IO文件处理程序,等等)是非常重要的(所以请不要回答这个问题:)),但一旦它的完成,有没有真正的理由完全退出我的应用程序?

I know that carefully disposing your shared resources (IO file handlers, etc.) is important (so please don't answer that:)), but once it's done, is there a real reason to cleanly exit my App ?

推荐答案

杀死进程将意味着finally块不被执行,而且可能甚至不是critical终结对象,这实际上可能是至关重要的,并导致资源的泄漏,在系统层面。它也可以在将来导致意想不到的错误,当别人保持code,因为他们(或你)将不得不拧头,不必觉得每次写一个finally块是否会被执行的时间。

Killing the process would mean that finally blocks will not be executed, and probably not even critical finalizer objects, which may actually be critical, and cause resource leakage at system level. It can also cause unexpected bugs in the future when someone else maintains the code, as they (or you) will have to twist their heads, having to think every time they write a finally block whether it will be executed.