Open In App

How to Terminate a Thread in C#

In C#, a thread can be terminated using Abort() method. Abort() throws ThreadAbortException to the thread in which it called. Due to this exception, the thread is terminated. There are two methods in the overload list of Thread.Abort Method as follows:

Abort()

This method raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Generally, this method is used to terminate the thread. Syntax:



public void Abort();

Exceptions:

Example: 






// C# program to illustrate the
// concept of Abort() method
// on a single thread
using System;
using System.Threading;
 
class ExampleofThread {
 
    // Non-Static method
    public void thread()
    {
        for (int x = 0; x < 3; x++) {
            Console.WriteLine(x);
        }
    }
}
 
// Driver Class
class ThreadExample {
 
    // Main method
    public static void Main()
    {
 
        // Creating instance for mythread() method
        ExampleofThread obj = new ExampleofThread();
 
        // Creating and initializing threads
        Thread thr = new Thread(new ThreadStart(obj.thread));
        thr.Start();
 
        Console.WriteLine("Thread is abort");
 
        // Abort the thread
        // Using Abort() method
        thr.Abort();
    }
}

Output:

Thread is abort

Explanation: The above example shows the use of Abort() method which is provided by the Thread class. By using thr.Abort(); statement, we can terminate the execution of the thread.

Abort(Object)

This method raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread while also providing exception information about the thread termination. Generally, this method is used to terminate the thread. Syntax:

public void Abort(object information);

Here, the information contains any information that you want to pass in a thread when it is being stopped. This information is only accessible by using ExceptionState property of ThreadAbortException. Exceptions:

Example: 




// C# program to illustrate the
// concept of Abort(object)
using System;
using System.Threading;
 
class ExThread {
 
    public Thread thr;
 
    public ExThread(string name)
    {
        thr = new Thread(this.RunThread);
        thr.Name = name;
        thr.Start();
    }
 
    // Entering point for thread
    void RunThread()
    {
        try {
            Console.WriteLine(thr.Name +
                        " is starting.");
 
            for (int j = 1; j <= 100; j++)
            {
                Console.Write(j + " ");
                if ((j % 10) == 0)
                {
                    Console.WriteLine();
                    Thread.Sleep(200);
                }
            }
            Console.WriteLine(thr.Name +
                  " exiting normally.");
        }
        catch (ThreadAbortException ex) {
            Console.WriteLine("Thread is aborted and the code is "
                                             + ex.ExceptionState);
        }
    }
}
 
// Driver Class
class GFG {
 
    // Main method
    static void Main()
    {
 
        // Creating object of ExThread
        ExThread obj = new ExThread("Thread ");
        Thread.Sleep(1000);
        Console.WriteLine("Stop thread");
        obj.thr.Abort(100);
 
        // Waiting for a thread to terminate.
        obj.thr.Join();
        Console.WriteLine("Main thread is terminating");
    }
}

Output:

Thread  is starting.
1 2 3 4 5 6 7 8 9 10 
11 12 13 14 15 16 17 18 19 20 
21 22 23 24 25 26 27 28 29 30 
31 32 33 34 35 36 37 38 39 40 
41 42 43 44 45 46 47 48 49 50 
Stop thread
Thread is aborted and the code is 100
Main thread is terminating

Important Points:

Reference:


Article Tags :
C#