Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Thread.Abort() work?

We usually throw exception when invalid input is passed to a method or when a object is about to enter invalid state. Let's consider the following example

private void SomeMethod(string value)
{
    if(value == null)
        throw new ArgumentNullException("value");
    //Method logic goes here
}

In the above example I inserted a throw statement which throws ArgumentNullException. My question is how does runtime manages to throw ThreadAbortException. Obviously it is not possible to use a throw statement in all the methods, even runtime manages to throw ThreadAbortException in our custom methods too.

I was wondering how do they do it? I was curious to know what is happening behind the scenes, I opened a reflector to open Thread.Abort and end up with this

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void AbortInternal();//Implemented in CLR

Then I googled and found this How does ThreadAbortException really work. This link says that runtime posts APC through QueueUserAPC function and that's how they do the trick. I wasn't aware of QueueUserAPC method I just gave a try to see whether it is possible with some code. Following code shows my try.

[DllImport("kernel32.dll")]
static extern uint QueueUserAPC(ApcDelegate pfnAPC, IntPtr hThread, UIntPtr dwData);
delegate void ApcDelegate(UIntPtr dwParam);

Thread t = new Thread(Threadproc);
t.Start();
//wait for thread to start
uint result = QueueUserAPC(APC, new IntPtr(nativeId), (UIntPtr)0);//returns zero(fails)
int error = Marshal.GetLastWin32Error();// error also zero

private static void APC(UIntPtr data)
{
    Console.WriteLine("Callback invoked");
}
private static void Threadproc()
{
    //some infinite loop with a sleep
}

If am doing something wrong forgive me, I have no idea how to do it. Again back to question, Can somebody with knowledge about this or part of CLR team explain how it works internally? If APC is the trick runtime follows what am doing wrong here?

like image 970
Sriram Sakthivel Avatar asked Aug 09 '13 19:08

Sriram Sakthivel


People also ask

What causes thread Abort exception?

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException on . NET Framework. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.

What is Abort method?

The Abort() method is used for destroying threads. The runtime aborts the thread by throwing a ThreadAbortException. This exception cannot be caught, the control is sent to the finally block if any. Use the Abort() method on a thread − childThread.Abort();

What is the difference between thread interrupt and thread Abort?

Abort method throws a ThreadAbortException, the Thread. Interrupt method throws a ThreadInterruptException. Essentially, a call to the Thread. Interrupt method interrupts the thread and throws a ThreadInterruptedException to interrupt the thread inside of a blocking call.


1 Answers

Are you sure you read the page you were pointing to? In the end it boils down to:

The call to Thread.Abort boils down to .NET setting a flag on a thread to be aborted and then checking that flag during certain points in the thread’s lifetime, throwing the exception if the flag is set.

like image 189
xanatos Avatar answered Oct 06 '22 03:10

xanatos