Advertisement



< Prev
Next >



C# Thread Abort() Method



When the Abort() method is called on a thread, it throws ThreadAbortException in the invoked thread, to begin the process of terminating the thread. In general, calling the Abort() on a thread, usually aborts or terminates its execution.

If the Abort() method is called on a thread before calling the Start() method on it, calling the Start() method on such a thread later on, will not start it, but throw the ThreadStartException in the thread which called the Abort() method and abort both the threads.

If the Abort() method is called on a thread which has started and is in either of the blocked states i.e. waiting state, sleep state or join state, will first interrupt the thread and then abort it.




Two overloaded versions of Abort() method


Method Description
public void Abort()
This constructor throws a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread.

public void Abort(Object exceptionInfo)
This constructor throws a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread and also provides an Object for exception information.






Calling the Abort() method on a running thread.


In this program, we are going to call the Abort() method on the running thread. This will throw the ThreadAbortException and abort the thread on which the Abort() method was called on.

Thread on calling the Abort() will throw a ThreadAbortException, so we will enclose its statements within a try-catch block to catch the exception.

//C# Example of Abort() method

using System;
using System.Threading;

class ThreadEx
{
Thread thread;

//Constructor of our class
public ThreadEx(String name)
{

	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Setting the name of a thread by using the Name property of Thread
	thread.Name = name;

	//Calling the Start() method, which calls the entry-point method defined with an argument
	thread.Start();
}

public void Run()
{
	//Getting the reference of the currently executing thread
	Thread th = Thread.CurrentThread;
	
	try
	{
		Console.WriteLine(th.Name + " has started its execution");
		for(int i=0; i<3; i++)
		{
			Console.WriteLine(th.Name + " is printing " + i);
		
			//Calling the Sleep() method on newly created thread
			//To make it sleep and suspend for 2 seconds after printing a number
			Thread.Sleep(1000);
		}
		Console.WriteLine(th.Name + " has finished its execution ");
	}
	catch(ThreadAbortException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Creating an object of our ThreadEx class
	//where, this object is used to construct a new child thread
	ThreadEx ob1 = new ThreadEx("My Thread1");

	//Making the main Thread sleep for 1 second
	//Giving the child thread enough time to start its execution
	Thread.Sleep(1000);

	//Main thread calling Abort() on the new child Thread
	//This will abort the new thread and throw ThreadAbortException in it
	ob1.thread.Abort();
}
}


Output-


My Thread1 has started its execution
My Thread1 is printing 0
Thread InterruptedSystem.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
   at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
   at ThreadEx.Run()



Advertisement




Calling the Abort() method on a thread which hasn't started yet.


In this program, we are going to call the Abort() method on a thread before calling the Start() method on it. Calling the Start() method on such a thread later on, will not start it, but throw the ThreadStartException in the thread which called the Abort() method and abort both the threads.

//C# The ThreatStartException is thrown if the Abort() method is called on a thread which hasn't started yet.
//And, the execution of the thread is aborted.

using System;
using System.Threading;

class ThreadEx
{
Thread thread;

//Constructor of ThreadEx
public ThreadEx(String name)
{

	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Setting the name of a thread by using the Name property of Thread
	thread.Name = name;
}

public void Run()
{
	//Getting the reference of the currently executing thread
	Thread th = Thread.CurrentThread;
	
	try
	{
		Console.WriteLine(th.Name + " has started its execution");
		for(int i=0; i<3; i++)
		{
			Console.WriteLine(th.Name + " is printing " + i);
		
			//Calling the Sleep() method on newly created thread
			//To make it sleep and suspend for 2 seconds after printing a number
			Thread.Sleep(1000);
		}
		Console.WriteLine(th.Name + " has finished its execution ");
	}
	catch(ThreadAbortException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	try
	{
		//Creating an object of our ThreadEx class
		//where, this object is used to construct a new child thread
		ThreadEx ob1 = new ThreadEx("My Thread1");


		//Main thread calling Abort() on the new child Thread, which hasn't started yet
		//This will leads to the ThreadStartException
		//And calling the Start() method on the same thread later on will abort it and throw ThreadStartException
		ob1.thread.Abort();

		//Calling the Start() method will not start the thread
		//but throw ThreadStartException and abort it.
		//Because the Abort() method was called on it before it could start
		ob1.thread.Start();

		Console.WriteLine("The Main Thread has terminated");
	}
	catch(ThreadStartException e)
	{
		Console.WriteLine("ThreadStartException is caught: " + e);
	}
}
}


Output-


ThreadStartException is caught: System.Threading.ThreadStartException: Thread failed to start. ---> System.Threading.ThreadAbortException: Thread was being aborted.
   --- End of inner exception stack trace ---
   at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
   at System.Threading.Thread.Start()
   at ThreadEx.Main(String[] ar)





Calling the Abort() method on a thread which is in blocked state.


If the Abort() method is called on a thread, which has started its execution and is in either of the blocked states i.e. waiting state, sleep state or join state, will first interrupt the thread and then abort it by throwing ThreadAbortException. Let us show you an example.

//Example of Abort() method

using System;
using System.Threading;

class ThreadEx
{
Thread thread;

//Constructor of our class
public ThreadEx(String name)
{
	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Setting the name of a thread by using the Name property of Thread
	thread.Name = name;

	//Calling the Start() method, which calls the entry-point method defined with an argument
	thread.Start();
}

public void Run()
{
	//Getting the reference of the currently executing thread
	Thread th = Thread.CurrentThread;
	
	try
	{
		Console.WriteLine(th.Name + " has started its execution");
		for(int i=0; i<3; i++)
		{
			Console.WriteLine(th.Name + " is printing " + i);
		
			//Calling the Sleep() method on newly created thread
			//To make it sleep and suspend for 2 seconds after printing a number
			Thread.Sleep(2000);
		}
		Console.WriteLine(th.Name + " has finished its execution ");
	}
	catch(ThreadAbortException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Creating an object of our ThreadEx class
	//where each object is used to construct a separate thread
	ThreadEx ob1 = new ThreadEx("My Thread1");

	//Making the Main thread sleep for 500 milliseconds i.e. half a second
	//Which gives enough time for its child start to start its execution
	//Thread.Sleep(500);

	//Main thread calling Abort() on the child Thread which is in a blocked state
	//will  throw ThreadAbortException 
	ob1.thread.Abort();
	
	//Main thread has called Join() method on the new thread
	//To wait until its execution is complete
	ob1.thread.Join();
}
}


Output-


My Thread1 has started its execution
My Thread1 is printing 0
Thread InterruptedSystem.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
   at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
   at ThreadEx.Run()





Please share this article -




< Prev
Next >
< C# ThreadStateException
C# Thread Synchronization >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement