当拥有的对象被收集时,如何终止工作线程?
我有一个具有 BackgroundWorker
线程的对象(纯粹是一个 Action 委托队列)。即,这是常见的、简单的单生产者单消费者场景。
当收集单个生产者时,我希望它将 Terminate 操作排入 BackgroundWorker
线程。
这听起来很简单 - 使用终结器 - 但这打破了“不要触摸终结器中的托管资源”规则。
那么,如何确保线程在没有更多工作要做时干净地终止呢?
我宁愿不接受的答案:
IDisposable
:这将需要对基类进行大规模的破坏性更改,但我接受它可能是必需的(这似乎总是 IDisposable 模式的问题.. )ThreadPool
:这些是长时间运行的操作,必须按顺序运行。所以我认为专用线程是合理的选择。WeakReference
:我刚刚想到了这个。也许这是执行此操作的正确方法(?)。基本上,线程将WeakReference
保留回所属对象,并定期唤醒自身以检查该WeakReference
是否仍然存在,当它死亡时,它会将 Terminate 排入队列。不太优雅 - 我不喜欢“定期唤醒自己”位 - 但这是最好的解决方案吗?
I have an object that has a BackgroundWorker
thread (purely a queue of Action delegates). i.e., it's the common, simple single-producer single-consumer scenario.
When the single producer is collected, I would like it to enqueue a Terminate action to the BackgroundWorker
thread.
It almost sounds easy - use a finalizer - but that breaks the "don't touch a managed resource in a finalizer" rule.
So how do I ensure the thread terminates cleanly once it has no more work to do?
Answers I'd rather not take:
IDisposable
: This would require a massive breaking change to the base class, but I accept it is perhaps required (this always seems to be a problem with the IDisposable pattern..)ThreadPool
: These are long running actions that must be run in order. So I would consider a dedicated thread to be the logical choice.WeakReference
: I just thought of this one. Perhaps it is the correct way to do this (?). Basically the Thread keeps aWeakReference
back to the owning object, and periodically wakes itself to check if thatWeakReference
is still alive, when it dies it enqueues a Terminate. Not exactly elegant - I don't like the "periodically wakes itself" bit - but is this the best solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IDisposable
或类似的东西听起来对我来说是最好的方法 - 明确地说出你何时完成生产,而不是从垃圾收集中触发它。从根本上来说,听起来问题并不是终止工作线程——它表明您已经完成了生产。我确实明白,在某些情况下这可能会很棘手,但如果可能的话,如果您能明确地做到这一点,那么您的生活将会变得更加可预测。
IDisposable
or something similar sounds like the best approach to me - explicitly say when you've finished producing, rather than triggering that from garbage collection.Fundamentally it sounds like the problem isn't terminating the worker thread - it's indicating that you've finished producing. I do understand that that can be tricky in some situations, but if at all possible it'll make your life more predictable if you can do that explicitly.