Are backgroundworker threads re-used?
后台工作线程是否重复使用?
Specifically, if I set a named data slot (thread-local storage) during the DoWork() method of a backgroundworker, will the value of that data slot persist, potentially to be found be some other thread at a later time?
具体来说,如果我在后台工作者的DoWork()方法期间设置了一个命名数据槽(线程本地存储),那么该数据槽的值是否会持续存在,以后可能会发现某个其他线程?
I wouldn't have thought so, but I have this bug...
我不会这么想,但我有这个错误......
EDIT: This blog post suggests that BackGroundWorker utilises a ThreadPool, which implies that Threads are re-used. So the question becomes; do re-used threads potentially persist thread-local storage between invocations?
编辑:这篇博文建议BackGroundWorker使用ThreadPool,这意味着线程被重用。所以问题就变成了;重用线程是否可能在调用之间持久保存线程本地存储?
2 个解决方案
#1
8
When the thread pool reuses a thread, it does not clear the data in thread local storage or in fields that are marked with the ThreadStaticAttribute attribute. Therefore, data that is placed in thread local storage by one method can be exposed to any other method that is executed by the same thread pool thread. A method that accesses a field that is marked with the ThreadStaticAttribute attribute could encounter different data depending on which thread pool thread executes it.
当线程池重用线程时,它不会清除线程本地存储中或使用ThreadStaticAttribute属性标记的字段中的数据。因此,通过一个方法放置在线程本地存储中的数据可以暴露给由同一线程池线程执行的任何其他方法。访问使用ThreadStaticAttribute属性标记的字段的方法可能会遇到不同的数据,具体取决于哪个线程池线程执行它。
source : http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx
来源:http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx
#2
-1
Would need to check the source (or via Reflector) to determine this if it is not specified in MSDN.
如果未在MSDN中指定,则需要检查源(或通过Reflector)以确定它。
If it isn't specified you can't rely on the current behaviour not changing in a future version of .NET.
如果未指定,则不能依赖于将来版本的.NET中不会更改的当前行为。
Edit: Looks like it is using the thread pool, so threads will be reused.
编辑:看起来它正在使用线程池,因此线程将被重用。
#1
8
When the thread pool reuses a thread, it does not clear the data in thread local storage or in fields that are marked with the ThreadStaticAttribute attribute. Therefore, data that is placed in thread local storage by one method can be exposed to any other method that is executed by the same thread pool thread. A method that accesses a field that is marked with the ThreadStaticAttribute attribute could encounter different data depending on which thread pool thread executes it.
当线程池重用线程时,它不会清除线程本地存储中或使用ThreadStaticAttribute属性标记的字段中的数据。因此,通过一个方法放置在线程本地存储中的数据可以暴露给由同一线程池线程执行的任何其他方法。访问使用ThreadStaticAttribute属性标记的字段的方法可能会遇到不同的数据,具体取决于哪个线程池线程执行它。
source : http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx
来源:http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx
#2
-1
Would need to check the source (or via Reflector) to determine this if it is not specified in MSDN.
如果未在MSDN中指定,则需要检查源(或通过Reflector)以确定它。
If it isn't specified you can't rely on the current behaviour not changing in a future version of .NET.
如果未指定,则不能依赖于将来版本的.NET中不会更改的当前行为。
Edit: Looks like it is using the thread pool, so threads will be reused.
编辑:看起来它正在使用线程池,因此线程将被重用。