Is thread-local storage persisted between backgroundworker invocations?

こ雲淡風輕ζ 提交于 2019-12-05 00:16:24

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.

source : http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx

Would need to check the source (or via Reflector) to determine this if it is not specified in MSDN.

If it isn't specified you can't rely on the current behaviour not changing in a future version of .NET.

Edit: Looks like it is using the thread pool, so threads will be reused.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!