Thread t = new Thread(new ThreadStart(DoSomething));
t.Start();
Console.WriteLine('Continuing with the execution...');
while (!t.IsAlive);
Thread.Sleep(1);
t.Abort();
Console.ReadLine();
}
static void DoSomething() {
try {
while (true) {
Console.WriteLine('Doing something...');
}
} catch (ThreadAbortException ex) {
Console.WriteLine(ex.Message);
}
}
}
When the thread is started, you continue with the next statement and print out the message 'Continuing with the execution...
'. You then use the IsAlive
property of the Thread class to find out the execution status of the thread and block the execution of the Main()
function (with the while statement) until the thread has a chance to start. The Sleep()
method of the Thread
class blocks the current thread (Main()
) for a specified number of milliseconds. Using this statement, you are essentially giving the DoSomething()
function a chance to execute. Finally, you kill the thread by using the Abort()
method of the Thread
class.
The ThreadAbortException
exception is fired on any thread that you kill. Ideally, you should clean up the resources in this exception handler (via the finally
statement):
static void DoSomething() {
try {
while (true) {
Console.WriteLine('Doing something...');
}
} catch (ThreadAbortException ex) {
Console.WriteLine(ex.Message);
} finally {
//---clean up your resources here---
}
}
The output of the preceding program may look like this:
Continuing with the execution...
Doing something...
Doing something...
Doing something...
Doing something...
Doing something...
Doing something...
Doing something...
Thread was being aborted.
Notice that I say the program
Doing something...
Continuing with the execution...
Doing something...
Doing something...
Doing something...
Doing something...
Doing something...
Doing something...
Thread was being aborted.
While you can use the Abort()
method to kill a thread, it is always better to exit it gracefully whenever possible.
Here's a rewrite of the previous program:
class Program {
static void Main(string[] args) {
Thread t = new Thread(new ThreadStart(DoSomething));
t.Start();
Console.WriteLine('Continuing with the execution...');
while (!t.IsAlive);
Thread.Sleep(1);
Console.ReadLine();
}
static void DoSomething() {
try {
Console.WriteLine('Doing something...');
}
} catch (ThreadAbortException ex) {
Console.WriteLine(ex.Message);
} finally {
//---clean up your resources here---
}
}
}
First, you declare a static Boolean variable call _stopThread
:
private static volatile bool _stopThread = false;
Notice that you prefix the declaration with the volatile
keyword, which is used as a hint to the compiler that this variable will be accessed by multiple threads. The variable will then not be subjected to compiler optimization and will always have the most up-to-date value.
To use the _stopThread
variable to stop the thread, you modify the DoSomething ()
function, like this:
while (!_stopThread) {
Console.WriteLine('Doing something...');
}