Multithreading basics in .Net Framework
Published: 04 Feb 2003 15:14 GMT
Aborting threads
You might, on occasion, require that a thread be terminated within another thread before it runs through to its normal end. In such a case, you would call the Abort() method. Normally, this method will permanently stop the execution of a specified thread:
Visual Basic
Overloads Public Sub Abort()
C#
public void Abort();
C++
public: void Abort();
Notice, I said normally. When a thread is requested to stop with the Abort() method, a ThreadAbortException is actually thrown within the thread. This exception, like any other, can be caught. But unlike most other exceptions, the ThreadAbortException is special: It gets rethrown at the end of the catch block unless the aborting thread's ResetAbort() method is called. Calling the ResetAbort() method cancels the abort, which, in turn, prevents the ThreadAbortException from stopping the thread.
Visual Basic
Public Shared Sub ResetAbort()
C#
public static void ResetAbort();
C++
public: static void ResetAbort();
Be aware that an aborted thread can't be restarted. If you attempt to do so, a ThreadStateException exception is thrown instead.
Joining threads
What if you want to execute something immediately after the threads finish? Or more generally, how do you handle the scenario where one thread needs to wait for another thread to complete before continuing? You need to join the threads using the Join() method:
Visual Basic
Overloads Public Sub Join()
C#
public void Join();
C++
public: void Join();
You can join threads by using one of the three overloaded Join() methods. The first overloaded method, as shown above, takes no parameters and waits until the thread completes. The second takes an Integer/int/Int32 parameter and then waits the parameter's specified number of milliseconds or for the thread to terminate, whichever is shorter. The third overload takes a TimeSpan and functions the same as the previous overload.
Interrupting threads
It's possible to take a worker thread that is waiting for something to happen and place it in a tight loop, waiting for that event to occur. But again, this would be a big waste of CPU cycles. It's better to let the worker thread sleep and then be woken up when the event occurs. We can do that using a combination of Sleep() and Interrupt() methods in conjunction with ThreadInterruptedException:
Visual Basic
Public Sub Interrupt()
C#
public void Interrupt();
C++
public: void Interrupt();
The basic idea is to put the worker thread to sleep using the Sleep() method and then to use the Interrupt() member method to interrupt the sleep of the worker thread when the required event occurs. Simple enough, except that the Interrupt() method throws a ThreadInterruptedException instead of just terminating the Sleep() method. Thus, you need to use exception handling and place the Sleep() method in a try block. Then, have the worker thread continue execution in the catch block.
For instance, in the worker thread, you'd have the following (pseudo) code:
Try
<< Wait for event to occur >>
catch << A ThreadInterruptedException >>
<< continue processing >>
You'd then have some other thread execute the Interrupt() method to get the above thread to continue.








