In response to Ian’s post on thread.abort, Richard Blewett points out a situation when the thread you are attempting to cancel can’t check the volatile book flag to determine whether it should cancel itself or not.
An example he presents is when the thread is waiting on a synchronization primitive. The solution given is to call Thread.Interrupt.
This is a handy technique when you have a reference to the thread you wish to cancel, but this is not often the case when dealing with asynchronous method calls such as spawned by calling
BeginInvoke. You won’t have a reference to the thread that an asynchronous method call is operating on.
So what is the would be thread terminator to do? Rather than go back in time and stop the thread from being spawned in the first place (my apologies for the poor cinema reference), avoid having indefinite waits on synchronization primitives in the first place. With a
ManualResetEvent for example, you can specify a timeout for the WaitOne method. I recommend that you do so.