In .Net you have the option of Thread.Suspend() and Thread.Abort(), but under normal circumstances these are horrible options to use because they’re likely to create locking problems (i.e., the thread has a locksÂ on resource that other threads need).
A far better way is to signal to the thread that it shouldÂ pause andÂ wait for a signal to continue. Sometimes it can be as simple as setting a boolean value where both threads can see it. Or you could wrap the boolean value in a property and protect it with a lock, but this is probably overkill for most applications.
To pause a thread safely requires you to use a blocking signal mechanism and .Net has ManualResetEvent for just this purpose.
You could then have some C# code like this:
ManualResetEvent pauseEvent = new ManualResetEvent();
//Thread A (Controlling thread)
//Thread B (the one to pause)
while(doWork == true)
//wait until event is set
This way, you can safely pause work and continue it at a later time, safely avoiding the possibility of deadlocks*.
* Actually, it’s still possible to create a resource locking problem–make sure you release any shared resource locks before pausing.
Check out my latest book, the essential, in-depth guide to performance for all .NET developers:
Writing High-Performance.NET Code by Ben Watson. Available now as an eBook at: