Is there a local thread store between background worker calls?

Are desktop themes repeated?

In particular, if I set up a named data slot (streaming local storage) during the DoWork () method of the background worker, will the value of this data slot be preserved, could you potentially find some other stream later?

I would not think so, but I have this error ...

EDIT: This blog post suggests BackGroundWorker uses ThreadPool , which implies that Threads are reused. So the question arises; Are reusable threads potentially storing local thread storage between calls?

+10
multithreading c # backgroundworker thread-local
Feb 18 '09 at 15:29
source share
3 answers

When a thread pool reuses a thread, it does not clear data in the local thread store or in fields marked with the ThreadStaticAttribute attribute. Therefore, data placed in a local thread store in one way can be subjected to any other method that is executed by the same thread pool thread. A method that accesses the field marked with the ThreadStaticAttribute attribute may encounter different data depending on which thread of the thread's thread executes it.

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

+7
Aug 05 '10 at 10:40
source share

Well, internally BackgroundWorker uses BeginInvoke , which means (I believe) that it uses one of the threads from ThreadPool . As for if TLS is cleared when threads return to the pool, I cannot say for sure.

In response to the editing, nothing I saw in Reflector says that the TLS slots are cleared or freed after returning to the pool. Therefore, you must explicitly clear them before the DoWork method DoWork .

+4
Feb 18 '09 at 15:36
source share

You need to check the source (or through Reflector) to determine if it is not specified in the MSDN.

If it is not specified, you cannot rely on current behavior that does not change in a future version of .NET.

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

0
Feb 18 '09 at 15:34
source share



All Articles