// This method will be called when the thread is started.publicvoid DoWork()
Console.WriteLine("worker thread: working...");
Console.WriteLine("worker thread: terminating gracefully.");
_shouldStop = true;
// Volatile is used as hint to the compiler that this data// member will be accessed by multiple threads.privatevolatilebool _shouldStop;
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
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:
// Request that the worker thread stop itself:
// Use the Join method to block the current thread // until the object's thread terminates.
Console.WriteLine("main thread: Worker thread has terminated.");