Join, Sleep And Abort In C# Threading

C# Sleep() method of Thread class is useful when you need to pause a program in C#. Code examples how to use Thread.Join(), Thread.Sleep(), and Thread.Abort() methods in C#.

Thread.Join() methid and Thread.Sleep() method

 
The Thread class consists of a number of methods, Join() and Sleep() methods are two of them. The Thread.Join() method is used to call a thread and blocks the calling thread until a thread terminates i.e Join method waits for finishing other threads by calling its join method.

The Thread.Sleep() method blocks the current thread for the specified number of milliseconds. In other words, we can say that it suspends the current thread for a specified time.

Here is a complete example:

  1. using System;  
  2. using System.Diagnostics;  
  3. using System.Threading;  
  4. namespace joinandsleep {  
  5.     classProgram {  
  6.         staticvoid Run() {  
  7.             for (int i = 0; i < 50; i++) Console.Write("C#corner");  
  8.         }  
  9.         staticvoid Main(string[] args) {  
  10.             Thread th = newThread(Run);  
  11.             th.Start();  
  12.             th.Join();  
  13.             Console.WriteLine("Thread t has terminated !");  
  14.             Console.Read();  
  15.         }  
  16.     }  
  17. }  

The output looks like this:

Join, Sleep And Abort Methods In C# Threading

 

This prints "C#corner" 50 times, after that "Thread t has terminated". You can include a timeout when calling Join, either in milliseconds or as a TimeSpan. It then returns true if the thread ended or false if it timed out.

We can include spacific time via thread.sleep() method like as:

  1. Thread.Sleep (TimeSpan.FromHours (1)); // sleep for 1 hour  
  2. Thread.Sleep (1000); // sleep for 1000 milliseconds  

While waiting on a Sleep() or Join(), a thread is blocked and so does not consume CPU resources.

Thread.Abort() method

The Thread.Abort() method is used to start the process of terminating the thread. This method is called to terminates the thread. The method raises the System.Threading.ThreadingAbortException in the thread in which it is invoked.

  1. using System;  
  2. using System.Threading;  
  3. using System.Diagnostics;  
  4. namespace workingofabort {  
  5.     classakshay {  
  6.         static Thread th;  
  7.         staticvoid ChildThread() {  
  8.             try {  
  9.                 throw newException();  
  10.             } catch (Exception) {  
  11.                 try {  
  12.                     Console.WriteLine("thread are going to sleep mode");  
  13.                     Thread.Sleep(5000);  
  14.                     Console.WriteLine("now thread is out of sleep");  
  15.                 } catch (ThreadAbortException e) {  
  16.                     Console.WriteLine(e.ToString());  
  17.                 }  
  18.             }  
  19.         }  
  20.         staticvoid Console_CancelKeyPress(Object sender, ConsoleCancelEventArgs e) {  
  21.             Console.WriteLine("aborting");  
  22.             if (th != null) {  
  23.                 | th.Abort();  
  24.                 th.Join();  
  25.             }  
  26.         }  
  27.         staticvoid Main(string[] args) {  
  28.             Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);  
  29.             th = newThread(ChildThread);  
  30.             th.Start();  
  31.             th.Join();  
  32.             Console.Read();  
  33.         }  
  34.     }  
  35. }  

Here is the output.

Join, Sleep And Abort Methods In C# Threading
 
Here is a detailed tutorial on Threads in C#.