Search content within the blog

Friday, April 24, 2009

How to: Create and Terminate Threads (C# Programming Guide)

This example demonstrates how an auxiliary or worker thread can be created and used to perform processing in parallel with the primary thread. Making one thread wait for another and gracefully terminating a thread are also demonstrated.

The example creates a class named Worker that contains the method that the worker thread will execute called DoWork. This is essentially the Main function for the worker thread. The worker thread will begin execution by calling this method, and terminate automatically when this method returns.


The Worker class contains an additional method that is used to indicate to DoWork that it should return. This method is called RequestStop.

The RequestStop method merely assigns the _shouldStop data member to true. Because this data member is checked by the DoWork method, this has the indirect effect of causing DoWork to return, thereby terminating the worker thread. However, it is important to note that DoWork and RequestStop will be executed by different threads. DoWork is executed by the worker thread, and RequestStop is executed by the primary thread, so the _shouldStop data member is declared volatile

he volatile keyword alerts the compiler that multiple threads will access the _shouldStop data member, and therefore it should not make any optimization assumptions about the state of this member.


using System;
using System.Threading;

public class Worker
{
// This method will be called when the thread is started.
public void DoWork()
{
while (!_shouldStop)
{
Console.WriteLine("worker thread: working...");
}
Console.WriteLine("worker thread: terminating gracefully.");
}
public void RequestStop()
{
_shouldStop = true;
}
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool _shouldStop;
}

public class WorkerThreadExample
{
static void Main()
{
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);

// Start the worker thread.
workerThread.Start();
Console.WriteLine("main thread: Starting worker thread...");

// Loop until worker thread activates.
while (!workerThread.IsAlive);

// Put the main thread to sleep for 1 millisecond to
// allow the worker thread to do some work:
Thread.Sleep(1);

// Request that the worker thread stop itself:
workerObject.RequestStop();

// Use the Join method to block the current thread
// until the object's thread terminates.
workerThread.Join();
Console.WriteLine("main thread: Worker thread has terminated.");
}
}


Sample Output

main thread: starting worker thread...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: terminating gracefully...
main thread: worker thread has terminated

No comments:

Post a Comment