Sleeping, Aborting, Suspending, Resuming, and Joining Threads

Besides the Start method, threads also have other methods that let you control their behavior:

  • Sleep— Makes a thread stop executing for a specified number of milliseconds.

  • Abort— Aborts a thread.

  • Suspend— Suspends a thread until you call Resume.

  • Resume— Resumes a suspended thread's execution.

  • Join— Returns only when the thread it's called on terminates.

We'll put these methods to work in a new example, ch15_02.cs. In this case, we'll have three threads, which will display "No Worries.", "No Problems." and "No Troubles." 20 times each. After creating these threads, we name them with the Thread class's Name property, and start thread1:

 Thread thread1 = new Thread(new ThreadStart(NoWorries)); ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.