C# 如何中止使用 ThreadPool.QueueUserWorkItem 创建的线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 08:16:11  来源:igfitidea点击:

How to abort threads created with ThreadPool.QueueUserWorkItem

提问by juan

is there a way to abort threads created with QueueUserWorkItem?

有没有办法中止使用 QueueUserWorkItem 创建的线程?

Or maybe I don't need to? What happens if the main application exits? Are all thread created from it aborted automatically?

或者也许我不需要?如果主应用程序退出会发生什么?从它创建的所有线程是否自动中止?

采纳答案by Judah Gabriel Himango

You don't need to abort them. When your application exits, .NET will kill any threads with IsBackground = true. The .NET threadpool has all its threads set to IsBackground = true, so you don't have to worry about it.

你不需要中止它们。当您的应用程序退出时,.NET 将终止 IsBackground = true 的所有线程。.NET 线程池的所有线程都设置为 IsBackground = true,因此您不必担心。

Now if you're creating threads by newing up the Thread class, then you'll either need to abort them or set their IsBackground property to true.

现在,如果您通过更新 Thread 类来创建线程,那么您需要中止它们或将它们的 IsBackground 属性设置为 true。

回答by Vaibhav

Yes, they will. However, if you are using unmanaged resources in those threads, you may end up in a lot of trouble.

是他们会。但是,如果您在这些线程中使用非托管资源,则最终可能会遇到很多麻烦。

回答by OJ.

The threadpool uses background threads. Hence, they will all be closed automatically when the application exits.

线程池使用后台线程。因此,当应用程序退出时,它们都会自动关闭。

If you want to abort a thread yourself, you'll have to either manage the thread yourself (so you can call Thread.Abort() on the thread object) or you will have to set up some form of notification mechanism which will let you tell the thread that it should abort itself.

如果你想自己中止一个线程,你要么自己管理线程(这样你就可以在线程对象上调用 Thread.Abort() )或者你必须设置某种形式的通知机制,让你告诉线程它应该中止自己。

回答by Will Dean

However, if you are using unmanaged resources in those threads, you may end up in a lot of trouble.

但是,如果您在这些线程中使用非托管资源,则最终可能会遇到很多麻烦。

That would rather depend how you were using them - if these unmanaged resources were properly wrapped then they'd be dealt with by their wrapper finalization regardless of the mechanism used to kill threads which had referenced them. And unmanaged resources are freed up by the OS when an app exits anyway.

这更取决于您如何使用它们 - 如果这些非托管资源被正确包装,那么无论用于终止引用它们的线程的机制如何,它们都将由它们的包装器终结处理。无论如何,当应用程序退出时,操作系统会释放非托管资源。

There is a general feeling that (Windows) applications spend much too much time trying to clean-up on app shutdown - often involving paging-in huge amounts of memory just so that it can be discarded again (or paging-in code which runs around freeing unmangaged objects which the OS would deal with anyway).

有一种普遍的感觉,(Windows) 应用程序在应用程序关闭时花费太多时间尝试清理 - 通常涉及分页大量内存,以便可以再次丢弃它(或运行的分页代码)释放操作系统无论如何都会处理的非托管对象)。

回答by Lukas ?alkauskas

yeah, they are background, but f.ex if you have application where you use ThreadPool for some kinda multiple downloading or stuff, and you want to stop them, how do you stop ? my suggestion would be: exit thread asap, f.ex

是的,它们是背景,但是 f.ex 如果您有使用 ThreadPool 进行某种多次下载或其他内容的应用程序,并且您想阻止它们,您如何停止?我的建议是:尽快退出线程,f.ex

bool stop = false;
void doDownloadWork(object s) 
{
   if (!stop)
   {
       DownloadLink((String)s, location);
   }
}

and if you set stop = true, second (currently in queue) threads automatically exit, after queue threads finishes it process.

如果您设置 stop = true,第二个(当前在队列中)线程会在队列线程完成处理后自动退出。

回答by Lukas ?alkauskas

According to Lukas ?alkauskas' answer.

根据 Lukas ?alkauskas 的回答。

But you should use:

但你应该使用:

volatile bool stop = false;

to tell the compiler this variable is used by several threads.

告诉编译器这个变量被多个线程使用。