using System; using System.Threading; class ThreadSleepApp { public static void WorkerThreadMethod() { Console.WriteLine("Worker thread started"); int sleepTime = 5000; Console.WriteLine("\tsleeping for {0} seconds", sleepTime / 1000); Thread.Sleep(sleepTime); // Sleep for five seconds. Console.WriteLine("\twaking up"); } public static void Main() { ThreadStart worker = new ThreadStart(WorkerThreadMethod); Console.WriteLine("Main - Creating worker thread"); Thread t = new Thread(worker); t.Start(); Console.WriteLine ("Main - Have requested the start of worker thread"); } }
There are two more ways to call the Thread.Sleep method. First, by calling Thread.Sleep with the value 0, you'll cause the current thread to relinquish the unused balance of its timeslice. Passing a value of Timeout.Infinite results in the thread being paused indefinitely until the suspension is interrupted by another thread calling the suspended thread's Thread.Interrupt method.
The second way to suspend the execution of a thread is by using the Thread.Suspend method. There are some major difference between the two techniques. First, the Thread.Suspend method can be called on the currently executing thread or another thread. Second, once a thread is suspended in this fashion, only another thread can cause its resumption, with the Thread.Resume method. Note that once a thread suspends another thread, the first thread is not blocked. The call returns immediately. Also, regardless of how many times the Thread.Suspend method is called for a given thread, a single call to Thread.Resume will cause the thread to resume execution.