Csharp/C Sharp/Thread/Thread Start Wait
Demonstates starting and waiting on a thread
<source lang="csharp"> /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794
- /
// Thread.cs -- Demonstates starting and waiting on a thread // // Compile this program with the following command line: // C:>csc Thread.cs using System; using System.Windows.Forms; using System.Threading; namespace nsThreads {
public class ThreadDemo { static public void Main () {
// Create the new thread object
Thread NewThread = new Thread (new ThreadStart (RunThread));
// Show a message box.
MessageBox.Show ("Click OK to start the thread", "Thread Start");
// Start the new thread.
NewThread.Start ();
// Inform everybody that the main thread is waiting
Console.WriteLine ("Waiting . . .");
// Wait for NewThread to terminate.
NewThread.Join ();
// And it"s done.
Console.WriteLine ("\r\nDone . . ."); }
// Method to assign to the new thread as its start method
static public void RunThread () {
// Sleep for a second, print and message and repeat.
for (int x = 5; x > 0; --x) { Thread.Sleep (1000); Console.Write ("Thread is running. {0} second{1} \r", x, x > 1 ? "s" : ""); }
// The thread will terminate at this point. It will not return to // the method that started it.
} }
}
</source>
Thread Sample
<source lang="csharp"> /* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765
- /
using System; using System.Threading; public class ThreadSample {
public static void Main() { ThreadSample ts = new ThreadSample(); } public ThreadSample() { int i; Thread newCounter = new Thread(new ThreadStart(Counter)); Thread newCounter2 = new Thread(new ThreadStart(Counter2)); newCounter.Start(); newCounter2.Start(); for(i = 0; i < 10; i++) { Console.WriteLine("main: {0}", i); Thread.Sleep(1000); } } void Counter() { int i; for (i = 0; i < 10; i++) { Console.WriteLine(" thread: {0}", i); Thread.Sleep(2000); } } void Counter2() { int i; for (i = 0; i < 10; i++) { Console.WriteLine(" thread2: {0}", i); Thread.Sleep(3000); } }
}
</source>
Threads:Waiting with WaitHandle
<source lang="csharp">
using System; using System.Threading; class ThreadSleeper {
int seconds; AutoResetEvent napDone = new AutoResetEvent(false); private ThreadSleeper(int seconds) { this.seconds = seconds; } public void Nap() { Console.WriteLine("Napping {0} seconds", seconds); Thread.Sleep(seconds * 1000); Console.WriteLine("{0} second nap finished", seconds); napDone.Set(); } public static WaitHandle DoSleep(int seconds) { ThreadSleeper ts = new ThreadSleeper(seconds); Thread thread = new Thread(new ThreadStart(ts.Nap)); thread.Start(); return(ts.napDone); }
} public class OperationsThreadsWaitingwithWaitHandle {
public static void Main() { WaitHandle[] waits = new WaitHandle[2]; waits[0] = ThreadSleeper.DoSleep(8); waits[1] = ThreadSleeper.DoSleep(4); Console.WriteLine("Waiting for threads to finish"); WaitHandle.WaitAll(waits); Console.WriteLine("Threads finished"); }
}
</source>