如何立即退出Windows窗体.NET应用程序? [英] How to immediately exit a Windows Forms .NET application?

查看:196
本文介绍了如何立即退出Windows窗体.NET应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序,我们有一个相当广泛的异常处理机制。在我们的错误处理逻辑的某些时候,我们要终止应用程序 - 就在这一点上,没有进一步的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.

这篇关于如何立即退出Windows窗体.NET应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆