Advertisement
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. |
//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();
}
}
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
//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);
}
}
}
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)
//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();
}
}
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
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement