是线程本地存储坚持BackgroundWorker的调用之间? [英] Is thread-local storage persisted between backgroundworker invocations?

查看:103
本文介绍了是线程本地存储坚持BackgroundWorker的调用之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现BackgroundWorker的线程再使用?

Are backgroundworker threads re-used?

特别是,如果我一个BackgroundWorker的DoWork的()方法过程中设置一个命名的数据槽(线程本地存储),将这些数据槽的值一直存在,潜在地被发现是在稍后的时间的一些其它线程?

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?

我不这样想,但我有这个错误...

I wouldn't have thought so, but I have this bug...

编辑:本博客文章表明,<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">BackGroundWorker利用线程池,这意味着线程的的重新使用。于是问题就变成了;不要重复使用线程可能持续调用之间的线程本地存储?

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 正在使用的BeginInvoke 此装置(我的认为的),它使用从线程池一个线程。至于如果在线程返回池中TLS被清除,我也说不准。

Well, internally BackgroundWorker is using BeginInvoke which means (I believe) that it is using one of the threads from the ThreadPool. As to if TLS is cleared when threads are returned to the pool, I can't say for certain.

在响应编辑,什么我见过的反射表明,TLS插槽被清除或释放一次返回到池中。所以,你有你的的DoWork 方法返回之前明确地清除它们。

In response to edit, nothing I have seen in Reflector suggests that TLS slots are cleared or freed once returned to the pool. So you have to explicitly clear them before your DoWork method returns.

这篇关于是线程本地存储坚持BackgroundWorker的调用之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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