V2 of the CLR no longer induces asynchronous Aborts (that is one thread
aborts another) while performing backout (catch, fault, filter or finaly
blocks). Note however that a host may always decide to escalate a CLR thread
Abort to a rude thread Abort, so be carefull when you rely on this, an
asynchronous thread abort should only be considered safe when being part of
a domain unload.
Willy.
|
| Dustin Campbell wrote:
| > It looks like the catch-block will be executed just like a finally block
| > will. Here's a sample console application that you can use to test this
behavior:
| >
| > using System;
| > using System.Threading;
| >
| > namespace ThreadTest
| > {
| > class Program
| > {
| > static ManualResetEvent resetEvent = new ManualResetEvent(false);
| >
| > static void ThreadProc()
| > {
| > string text = null;
| > try
| > {
| > Console.WriteLine(text.ToString());
| >
| > Console.WriteLine("ThreadProc(): Code at end of try");
| > }
| > catch (NullReferenceException)
| > {
| > Console.WriteLine("ThreadState at start of catch-block: {0}",
Thread.CurrentThread.ThreadState);
| > resetEvent.Set();
| > Thread.Sleep(1000);
| > Console.WriteLine("ThreadState at end of catch-block: {0}",
Thread.CurrentThread.ThreadState);
| > }
| > finally
| > {
| > Console.WriteLine("ThreadProc(): finally-block executed");
| > }
| >
| > Console.WriteLine("ThreadProc(): Code after finally-block");
| > }
| > static void Main(string[] args)
| > {
| > Thread t = new Thread(ThreadProc);
| > t.Start();
| > resetEvent.WaitOne();
| > t.Abort();
| > Console.WriteLine("Main(): Thread.Abort called");
| > }
| > }
| > }
| >
| > This should be the output from this test app:
| >
| > ThreadState at start of catch-block: Running
| > Main(): Thread.Abort called
| > ThreadState at end of catch-block: AbortRequested
| > ThreadProc(): finally-block executed
| >
| > Note that the code at the end of the try block and after the finally
block
| > is never executed. But, the code in the catch-blcok after the main
thread
| > is signaled does execute even though the main thread calls
Thread.Abort().
| > In fact, it demonstrates that the call to Abort has set the ThreadState
to
| > AbortRequested.
| >
| > Best Regards,
| > Dustin Campbell
| > Developer Express Inc.
|
| Hi Dustin,
|
| I know the following are true according to the spec of .net:
| 1) if the ThreadAbortException happens in the try block, the remaining
| of the try block should not run. But the finally block will run
| regardless.
| 2) if the ThreadAbortException happens in the finally block, the
| remaining of the finally block should run (in fact, .NET 1.1
| implemented it incorrectly. It seems to be fixed in .NET 2.0)
|
| The question is, if ThreadAbortException happens in the catch block,
| should the remaining of the catch block run or not according to the
| spec? Or in another word, does Microsoft implement the
| ThreadAbortException handling correctly in .NET 2.0 when it occurs in
| the catch block of the child thread?
|
| Thanks,
| Bill
|