views:

132

answers:

3

Hi, 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.

+2  A: 

Use the Backgroundworker which has a completed event

Sebastian Sedlak
+1  A: 

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.

Keltex
Polling for the status has been deprecated a long, long time ago, use events.
arul
A: 

Use a PostSharp attribute on the method.

GregC