Join, Sleep And Abort In C# Threading

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:

using System;  
using System.Diagnostics;  
using System.Threading;  
namespace joinandsleep {  
    classProgram {  
        staticvoid Run() {  
            for (int i = 0; i < 50; i++) Console.Write("C#corner");  
        }  
        staticvoid Main(string[] args) {  
            Thread th = newThread(Run);  
            th.Start();  
            th.Join();  
            Console.WriteLine("Thread t has terminated !");  
            Console.Read();  
        }  
    }  
}

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:

Thread.Sleep (TimeSpan.FromHours (1)); // sleep for 1 hour  
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.

using System;  
using System.Threading;  
using System.Diagnostics;  
namespace workingofabort {  
    classakshay {  
        static Thread th;  
        staticvoid ChildThread() {  
            try {  
                throw newException();  
            } catch (Exception) {  
                try {  
                    Console.WriteLine("thread are going to sleep mode");  
                    Thread.Sleep(5000);  
                    Console.WriteLine("now thread is out of sleep");  
                } catch (ThreadAbortException e) {  
                    Console.WriteLine(e.ToString());  
                }  
            }  
        }  
        staticvoid Console_CancelKeyPress(Object sender, ConsoleCancelEventArgs e) {  
            Console.WriteLine("aborting");  
            if (th != null) {  
                | th.Abort();  
                th.Join();  
            }  
        }  
        staticvoid Main(string[] args) {  
            Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);  
            th = newThread(ChildThread);  
            th.Start();  
            th.Join();  
            Console.Read();  
        }  
    }  
}

Here is the output.

Join, Sleep And Abort Methods In C# Threading

Here is a detailed tutorial on Threads in C#.


Similar Articles