English 中文(简体)
在收集自己的物品时,我如何终止工人的read子?
原标题:How do I terminate a worker thread when the owning object is collected?

我的物体有<条码>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 a WeakReference back to the owning object, and periodically wakes itself to check if that WeakReference 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?
最佳回答

<代码>可支配或类似于对我的最佳方法的类似声音——在你完成生产时明确说,而不是诱发垃圾收集。

从根本上说,它像问题一样,使工人的read子消失了,这表明你已经完成了生产。 我确实理解,在某些局势中,这可能是骗局的,但如果在all上,如果你能够明确地这样做,就会使你的生活更加可预测。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签