<span style="color: blue;">using</span> System;
<span style="color: blue;">using</span> System.Threading;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> Worker
{
<span style="color: green;">// This method is called when the thread is started.</span>
<span style="color: blue;">public</span> <span style="color: blue;">void</span> DoWork()
{
<span style="color: blue;">while</span> (!_shouldStop)
{
Console.WriteLine(<span style="color: #a31515;">"Worker thread: working..."</span>);
}
Console.WriteLine(<span style="color: #a31515;">"Worker thread: terminating gracefully."</span>);
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> RequestStop()
{
_shouldStop = <span style="color: blue;">true</span>;
}
<span style="color: green;">// Keyword volatile is used as a hint to the compiler that this data</span>
<span style="color: green;">// member is accessed by multiple threads.</span>
<span style="color: blue;">private</span> <span style="color: blue;">volatile</span> <span style="color: blue;">bool</span> _shouldStop;
}
<span style="color: blue;">public</span> <span style="color: blue;">class</span> WorkerThreadExample
{
<span style="color: blue;">static</span> <span style="color: blue;">void</span> Main()
{
<span style="color: green;">// Create the worker thread object. This does not start the thread.</span>
Worker workerObject = <span style="color: blue;">new</span> Worker();
Thread workerThread = <span style="color: blue;">new</span> Thread(workerObject.DoWork);
<span style="color: green;">// Start the worker thread.</span>
workerThread.Start();
Console.WriteLine(<span style="color: #a31515;">"Main thread: starting worker thread..."</span>);
<span style="color: green;">// Loop until the worker thread activates.</span>
<span style="color: blue;">while</span> (!workerThread.IsAlive) ;
<span style="color: green;">// Put the main thread to sleep for 1 millisecond to</span>
<span style="color: green;">// allow the worker thread to do some work.</span>
Thread.Sleep(1);
<span style="color: green;">// Request that the worker thread stop itself.</span>
workerObject.RequestStop();
<span style="color: green;">// Use the Thread.Join method to block the current thread </span>
<span style="color: green;">// until the object's thread terminates.</span>
workerThread.Join();
Console.WriteLine(<span style="color: #a31515;">"Main thread: worker thread has terminated."</span>);
}
<span style="color: green;">// Sample output:</span>
<span style="color: green;">// Main thread: starting worker thread...</span>
<span style="color: green;">// Worker thread: working...</span>
<span style="color: green;">// Worker thread: working...</span>
<span style="color: green;">// Worker thread: working...</span>
<span style="color: green;">// Worker thread: working...</span>
<span style="color: green;">// Worker thread: working...</span>
<span style="color: green;">// Worker thread: working...</span>
<span style="color: green;">// Worker thread: terminating gracefully.</span>
<span style="color: green;">// Main thread: worker thread has terminated.</span>
}
from:http://msdn.microsoft.com/zh-cn/library/x13ttww7.aspx