C# Thread Join, IsAlive, Abort

C# Code Snippets Thread Join IsAlive Abort
Share:

About

In this code snippet, we will take a look at thread join, isAlive and abort in C#.

join() The main(parent) thread waits for the joined threads(threads that had the join() method called on them) to complete their execution before proceeding with its execution.

abort() Can be used to abort the thread execution. For example, if a thread takes to long to finish we can abort its execution. When this method is called on a thread it will throw a ThreadAbortException that will stop the thread. However, it’s not guaranteed that a thread will stop in all situations.

isAlive() Checks if the thread is running and returns true or false.

Let’s have a look at the code below to see how to use thread join, isAlive and abort.

Code:

using System;
using System.Threading;

namespace Threading
{
    class Program
    {
        static void Main(string[] args)
        {
            //Thread Join//////////////////////////////////////////////////////////

            Thread th1 = new Thread(() => printToConsole(1));
            Thread th2 = new Thread(() => printToConsole(2));
            
            th1.Start();
            th2.Start();
            
            //With Join() the main(parent) thread waits for the joined threads to compleate their execution before proceeding. 
            th1.Join();
            th2.Join();

            Console.WriteLine("");

            ///////////////////////////////////////////////////////////////////////



            //Thread Abort////////////////////////////////////////////////////////

            Thread th3 = new Thread(() => printToConsole(3));
            th3.Start();

            //If the joined thread takes more than the specified amount of time(500ms in this case) join() will return false else it will return true.
            //In this case if the thread doesn't compleate in 500ms we will abort it.
            if (th3.Join(500))
            {
                Console.WriteLine("Thread has finished.");
            }
            else
            {
                Console.WriteLine("Thread took to long to execute, aborting...");
                th3.Abort();
            }

            Console.WriteLine("");

            //////////////////////////////////////////////////////////////////////



            //Thread Status///////////////////////////////////////////////////////

            Thread th4 = new Thread(() => printToConsole(4));
            th4.Start();
            
            /*
            //Check if thread is still running. 
            if (th4.IsAlive)
            {
                Console.WriteLine("Thread hasn't finished yet .");
            }
            else
            {
                Console.WriteLine("Thread has finished.");              
            }
            */

            threadStatus(th4);

            Console.WriteLine("");
            Console.WriteLine("Main thread finished.");

            //////////////////////////////////////////////////////////////////////

            Console.ReadLine();
        }

        public static void printToConsole(int threadNumber)
        {
            Thread.Sleep(1000);
            Console.WriteLine("Thread " + threadNumber + " finished.");
        }

        public static void threadStatus(Thread thread)
        {
            while (thread.IsAlive)
            {
                Console.WriteLine("Thread is still running.");
                Thread.Sleep(200);         
            }

            Console.WriteLine("Thread is done.");
        }
    }
}

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger