我的物体有<条码>BackgroundWorker(即行动代表的地址)。 i. 即单一生产者单一消费模式。
在收集单一生产商时,我希望将一项终止行动安排到<代码>BackgroundWorker 深处。
这几乎是容易的——使用最后审定人——但打破了“放之机”规则中的管理资源。
因此,如果我不再工作,我如何确保read子完全终止?
答案一:
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?