Pausing thread has been recognized as dangerous and is not available in .NET.
Here is what you need to use instead of Pause/Resume: one of the classes:
System.Threading.EventWaitHandle
,
System.Threading.AutoResetEvent
or
System.Threading.ManualResetEvent
; please see:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx[
^].
The first class can be used instead the other two; they are only different in the way how the event handle is reset: automatically or only explicitly.
The idea is this: the event has two states, signaled or non-signaled. One can signal the event by the call to
Set
and un-signal calling
Reset
or it will be reset automatically by a waiting thread. Now, you pause the thread by calling
EventWaitHandle.WaitOne
by the thread to be paused. If the event is non-signaled, the thread is put in the wait state: it is switched out by the OS and never scheduled back to execution until it is awaken. It will be awaken if
some other thread calls
Set
on the
same instance of event wait handle; it also will be awaken by a timeout (optional wait parameter) or
Thread.Abort
.
When a thread is awaken and the event handle uses auto-reset, it will be reset back to non-signaled state immediately on the wake-up of the thread. This transition is
atomic and cannot be reliably simulated by other methods (such as Boolean condition). This auto-reset mechanism is designed to pass one and only one waiting thread at a time and make other threads waiting on the same event handle instance in the wait state.
—SA