简体   繁体   English

正确的方法关闭前台线程

[英]Correct way to close a foreground thread

When closing an application, what is the best way to close foreground threads at the same time? 关闭应用程序时,同时关闭前台线程的最佳方法是什么?

From all my research, it seems to be that the best way is to set IsBackground to true.. 根据我的所有研究,似乎最好的方法是将IsBackground设置为true。

Is this correct or is there a better way? 这是正确的还是有更好的方法? I'm skeptical as it doesn't seem to follow Microsofts normally descriptive method name pattern. 我对此表示怀疑,因为它似乎没有遵循Microsoft通常描述性的方法名称模式。

It will certainly work, the CLR will abort a thread like that when your main startup exits. 它肯定会工作,当您的主启动退出时,CLR将中止类似的线程。 Pretty similar to using Thread.Abort(), minus the drastic failure modes you'd normally suffer from using Abort(). 与使用Thread.Abort()非常相似,减去使用Abort()通常会遇到的严重故障模式。 It is a rude abort, there's nothing that the thread itself can do to stop it. 这是一个不礼貌的中止,线程本身无法停止它。 And there will be no more code that runs afterwards that could fail due to the typical problems you'd get from Abort(), like a deadlock. 而且,由于您从Abort()获得的典型问题(如死锁),以后不会再运行任何可能失败的代码。 Other than finalizers. 除了终结器。

Calling it "correct" is however a bit of a stretch, there's also nothing that the thread can do to terminate cleanly. 然而,称它为“正确”有点费劲,线程也无法做任何干净地终止的事情。 Which might be detrimental if it has externally observable behavior. 如果它具有外部可观察到的行为,则可能是有害的。 Like updating a dbase, talking over a socket or writing a file. 就像更新dbase,通过套接字交谈或写入文件一样。 That's rudely interrupted as well, potentially leaving a confused server or a half-written file that can cause trouble later. 这也被粗暴地打断了,有可能留下混乱的服务器或半写的文件,这可能会在以后引起麻烦。 A mitigating circumstance is that this will also happen when your program dies on an unhandled exception, you'd expect it to not cause trouble either. 减轻影响的情况是,当程序因未处理的异常而死亡时,也会发生这种情况,您希望它也不会引起麻烦。 It depends :) 这取决于 :)

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

相关问题 Windows中是否有一种方法可以为具有DPI感知功能的前台应用程序获取正确的矩形? - Is there a way in Windows that can get the correct rectangle for the foreground application with DPI awareness? 什么是关闭TCP连接的正确方法 - What is the correct way to close a TCP connection 发生异常时关闭数据库连接的正确方法 - Correct way to close database connection in event of exception 关闭第二个Application循环的正确方法是什么? - What is the correct way to close a second Application loop? 正确关闭WCF 4通道的方法 - Correct way to close WCF 4 channels effectively 关闭进程的其他实例的正确方法? - Correct way to close down additional instances of a process? 关闭 WCF 线程的最佳方法是什么? - Whats the best way to close a WCF thread? 如果有一些前台线程在控制台上打印内容,那么在主线程完成工作后,控制台窗口是否会关闭? - Will the console window close after the main thread have done his work if there are some foreground threads that print something on the console? 处置传递给线程的对象的正确方法是什么? - What is the correct way to dispose an object passed to a thread? 将数据传递到正在运行的线程的正确方法是什么 - What is the correct way to pass data to a running thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM