如何立即退出Windows窗体.NET应用程序?窗体、应用程序、Windows、NET

2023-09-03 21:03:16 作者:痞味小仙女

在我们的应用程序,我们有一个相当广泛的异常处理机制。在我们的错误处理逻辑的某些时候,我们要终止应用程序 - 就在这一点上,没有进一步的code执行。

In our application, we have a quite extensive exception handling mechanism. At some point in our error handling logic, we want to terminate the application -- right at this point with no further code execution.

我们目前的code使用Environment.Exit()来做到这一点。调用Environment.Exit()后,仍执行一些code。例如,GC可以执行某些对象的终结(并且在我们的情况下,会导致一个问题)。我们不希望这样的事情发生。有没有办法来真的杀了我们自己的进程(的Win32 API调用可能)?

Our current code use Environment.Exit() to do that. After a call to Environment.Exit(), some code is still executed. For instance, the GC may execute the finalizer of some objects (and that causes a problem in our case). We don't want that to happen. Is there a way to really kill our own process (a Win32 API call maybe)?

当然,我们不希望最终用户看到弹出的Windows对话框时,程序崩溃...

Of course, we don't want the end-user to see the Windows dialog that appears when a program crashes...

推荐答案

这出现在一个简单的应用程序工作,但我还没有尝试过与任何复杂的:

This appears to work in a simple app, but I haven't tried it with anything complex:

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

GetCurrentProcess :

获取一个新的过程组成部分,   与当前关联它   活动过程。

Gets a new Process component and associates it with the currently active process.

杀:

杀强制终止   进程,同时 CloseMainWindow 只   请求终止。

Kill forces a termination of the process, while CloseMainWindow only requests a termination.