简体   繁体   English

如何杀死主线程?

[英]How to kill the main thread?

Strange issue. 奇怪的问题。 I kill the main thread, but when the program ends, it still processes the code after this.Close() . 我杀了主线程,但是当程序结束时,它仍然会在this.Close()之后处理代码。

Any ideas on how to stop that? 关于如何阻止它的任何想法?

MessageBox.Show("Servers overloaded. Exiting.");
this.Close();
//...other code that processes even after it closes...

Closing the window does not stop the thread. 关闭窗口不会停止线程。

You can use Environment.Exit to shutdown immediately. 您可以使用Environment.Exit立即关闭。

Unlike ASP.Net's Response.End() , calling Close will not abort the thread. 与ASP.Net的Response.End() ,调用Close不会中止该线程。

You need to add return; 你需要添加return; after calling Close() to exit the method. 在调用Close()之后退出方法。

Why would it stop at Close() ? 为什么它会在Close()处停止? It is still going to exit your method - most things like Close() are going to involve sending something to the windows message queue and having it processed. 它仍然会退出你的方法 - 像Close()这样的大多数东西都会涉及向windows消息队列发送一些内容并对其进行处理。 If you need to exit NOW , then perhaps Environment.FailFast ... 如果您需要也许现在退出,然后Environment.FailFast ...

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

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