Join, Sleep and Abort methods in C# Threading


join and sleep

The thread class consists of a number of methods, the 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. 

Whenever the thread.sleep() method are generally used to Block 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.

Example

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

Output :

bb.gif

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.

abort()

The thread.abort() method are used to start the process of terminating the thread. we are calling this method usually terminates the thread. it raised a System.Threading.ThreadingAbortException in the thread on which it is invoked.

using System;
using System.Threading;
using System.Diagnostics; 
namespace workingofabort
{
    class akshay
 {
        static Thread th;
        static void ChildThread()
        {
            try
            {
                throw new Exception();
            }
            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());
                }
            }
        }
        static void Console_CancelKeyPress(Object sender, ConsoleCancelEventArgs e)
        {
           Console.WriteLine("aborting");
            if (th != null)
            {|
               th.Abort();
                th.Join();
            }
        }       
       static void Main(string[] args)
        {
            Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
           th = new Thread(ChildThread);
           th.Start();
            th.Join();
            Console.Read();
        }
    }
}

Output

aa

Up Next
    Ebook Download
    View all
    Learn
    View all