无论如何检测线程方法是否已完成
我有一个队列,它基本上是 albahari.com 中的生产者/消费者队列 线程书,它从队列中取出一个队列项,该队列项是从队列中取出一个动作执行块,然后调用一个方法来执行队列项内的动作列表。
我可以通过排队空操作块轻松地杀死队列,并通过使用 thread.sleep() 执行旋转/等待来阻塞主线程,直到队列计数变为零,并且所有线程都通过 while !=null 循环,从而完成,但是执行最后一个actionlist的方法可能仍在执行最后一个线程。
问题是,他们无论如何都要检测该方法是否仍有线程执行它,就像使用信号量或带有互锁的计数器来在方法开始时对信号量进行计数并在方法结束时进行向下计数一样。 因此,如果它达到零,我就知道它没有线程。
这是在接口上实现一个 destroy 方法,在调用 dispose 之前将其关闭。
I've got a queue, which is basically the producer/consumer queue in the albahari.com
threading book, which takes an queue item off the queue, which is an action execution block off the queue and then calls a method to execute the actionlist within the queue item.
I can kill the queue easily enough by enqueing a null actionblock and block the main thread by doing a spin/wait with a thread.sleep() until the queue count goes to zero, and all the threads fall through the while !=null loop, thereby completing, but the method executing the last actionlist may still be executing the last thread.
Question is, is their anyway to detect if that method still has a thread executing it, like maybe using a Semaphore or counter with an Interlock to count the semaphore up at the beginning of the method and count it down at the end. So if it reaches zero, them I know its not threaded.
This is the implementing a destroy method on the interface, to close it down before calling dispose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用具有已完成事件的Backgroundworker
Use the Backgroundworker which has a completed event
如果您像这样启动一个线程:
您可以
在任何时候检查: 以确定其状态。
If you start a thread like this:
You can check:
at any point to determine its status.
在方法上使用 PostSharp 属性。
Use a PostSharp attribute on the method.