无论如何检测线程方法是否已完成

发布于 2024-07-16 23:13:45 字数 406 浏览 4 评论 0原文

我有一个队列,它基本上是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心意如水 2024-07-23 23:13:45

使用具有已完成事件的Backgroundworker

Use the Backgroundworker which has a completed event

溺孤伤于心 2024-07-23 23:13:45

如果您像这样启动一个线程:

System.Threading.Thread mythread = new System.Threading.Thread();
mythread.Start();

您可以

mythread.IsAlive()

在任何时候检查: 以确定其状态。

If you start a thread like this:

System.Threading.Thread mythread = new System.Threading.Thread();
mythread.Start();

You can check:

mythread.IsAlive()

at any point to determine its status.

夜深人未静 2024-07-23 23:13:45

在方法上使用 PostSharp 属性。

Use a PostSharp attribute on the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文