The followingCodeThe example shows the simple thread processing function.
Using system;
Using system. Threading;
// Simple threading scenario: Start a static method running
// On a second thread.
Public class threadexample {
// The threadproc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// The rest of its time slice each time, and then ends.
Public static void threadproc (){
For (INT I = 0; I <10; I ++ ){
Console. writeline ("threadproc: {0}", I );
// Yield the rest of the time slice.
Thread. Sleep (0 );
}
}
Public static void main (){
Console. writeline ("main thread: Start a second thread .");
// The constructor for the Thread class requires a threadstart
// Delegate that represents the method to be executed on
// Thread. C # simplifies the creation of this delegate.
Thread t = new thread (New threadstart (threadproc ));
// Start threadproc. On a uniprocessor, the thread does not get
// Any processor time until the main thread yields. uncomment
// The thread. Sleep that follows T. Start () to see the difference.
T. Start ();
// Thread. Sleep (0 );
For (INT I = 0; I <4; I ++ ){
Console. writeline ("main thread: Do some work .");
Thread. Sleep (0 );
}
console. writeline ("main thread: Call join (), to wait until threadproc ends. ");
T. join ();
console. writeline ("main thread: threadproc. join has returned. press enter to end program. ");
console. readline ();
}< BR >}