关闭代码中的当前应用程序(立即终止) [英] Close current application in code (terminate it immediatly)

查看:78
本文介绍了关闭代码中的当前应用程序(立即终止)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码"Application.Exit()"不会终止程序.有没有其他方法可以立即终止程序?该代码位于一个大概在应用程序主窗体之前创建的对象中.此对象确实进行资源检查和激活检查.

My code, ''Application.Exit()'' does not terminate the program. Is there anouther way to do this, to immediatly terminate the program? This code is in an object that is created presumably before the application''s main form. This object does recource checking and activation checks.

推荐答案

digimanus的解决方案有效,但需要解释,所引用的文章也很糟糕.

System.Windows.Forms.Application的退出方法实际上与应用程序正在运行的进程的终止无关.它们只会导致方法Application.Run返回.

方法Application.Exit退出应用程序,但完成了消息泵送,因此Application.Run不会立即返回.方法Application.ExitThread从循环返回;因此,它会在分派当前消息并执行返回到循环时立即发生.

这些方法仅影响一个线程,即UI线程.因此,如果还有另一个线程正在执行,则该过程不会终止.这是正常" Application.Exit之后执行该过程的最典型原因.方法System.Environment.Exit仍然可以工作.但是,我不建议这样做.线程应正确终止或正确中止,因为中止的线程中的某些后处理可能很重要. System.Environment.Exit是粗心的方式.准确退出的最佳方法是关闭主窗体并执行所有后处理,包括在其FormClosed事件中触发的线程终止.
请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.forms.application.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.environment. exit.aspx [ ^ ].

-SA
The solution by digimanus works, but it needs explanation, and the article referenced is pretty bad.

The exit methods of System.Windows.Forms.Application actually have nothing to do with the termination of the process where application is running. They only cause the method Application.Run to return.

The method Application.Exit exits the application but completes message pumping, so Application.Run does not return immediately. The method Application.ExitThread returns from the loop; so it happens as soon as the current message is dispatched and execution returns to the loop.

These method affect only one thread, the UI thread. So, the process is not terminated if there is another thread which is kept executing. This is the most typical reason of execution of the process after "normal" Application.Exit. The method System.Environment.Exit works anyway. However, I would not recommend it. The threads should be correctly terminated or correctly aborted because some post-processing in the aborted thread can be important. System.Environment.Exit is a careless way. The best way of accurate exit is closing of the main form and doing all post-processing including thread termination triggered in its FormClosed event.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.exit.aspx[^].

—SA


使用Environment.Exit参见此处:
use Environment.Exit see here: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx[^]


尝试Application.ExitThread函数.
Try Application.ExitThread function.


这篇关于关闭代码中的当前应用程序(立即终止)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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