线程池,.IsBackground在.NET [英] Thread Pool and .IsBackground in .NET

查看:229
本文介绍了线程池,.IsBackground在.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN,以及许多其他来源,声称工作线程在线程池总是背景

MSDN, as well as many other sources, claim that worker threads in the thread pool are always background.

线程池中的线程都是后台线程。 (MSDN)

"Thread pool threads are background threads." (MSDN)

池线程始终是后台线程。 (线程在C#中,约瑟夫阿尔巴哈利)

"Pooled threads are always background threads." (Threading in C#, Joseph Albahari)

我可以很容易地通过设置使工作线程非背景

I can easily make the worker thread non-background by setting

Thread.CurrentThread.IsBackground = false;

和应用程序将被等待,直到线程结束。

And the application will be waiting until the thread finishes.

有什么不对吗?

推荐答案

在做线程结束?当你的方法结束?我高度怀疑是这种情况。线程池的全部意义在于,一旦你的线程完成,它被放回池中被重用。一个线程现在你已经放开了,就不见了回线程池,你的应用程序仍在运行,因为它是一个前台线程。有没有办法让该线程退了出来杀了它。

When does the thread finish? When your method ends? I highly doubt that's the case. The whole point of the thread pool is that once your thread is finished, it gets put back in the pool to be reused. Now you've let go of a thread, it's gone back into the thread pool and your application is still running because it's a foreground thread. There's no way to get that thread back out to kill it.

这篇关于线程池,.IsBackground在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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