GOTCHA 54 ResetAbort() may lead to surprises


GOTCHA #54 ResetAbort() may lead to surprises

In Gotcha #52, "ThreadAbortExceptiona hot potato," you saw the special nature of THReadAbortException. The Thread class provides the interesting method ResetAbort() to overrule this exception. Let's start by reviewing the code in Example 7-6.

Example 7-6. ResetAbort() at work

C# (ResetAbortMethod)

 using System; using System.Threading; namespace ResetAbort {     class Test     {         private static void Worker()         {             try             {                 Thread.Sleep(5000);             }             catch(ThreadAbortException)             {                 Console.WriteLine(                     "ThreadAbortException caught");                 Thread.ResetAbort();             }             Console.WriteLine("Look where we are now!");             Thread.Sleep(10000);         }         [STAThread]         static void Main(string[] args)         {             Thread workerThread                 = new Thread(new ThreadStart(Worker));             workerThread.Start();             Thread.Sleep(1000);             Console.WriteLine("Calling abort");             workerThread.Abort();             Thread.Sleep(2000);             Console.WriteLine("Main done");         }     } } 

VB.NET (ResetAbortMethod)

 Imports System.Threading Module Test     Private Sub Worker()         Try             Thread.Sleep(5000)         Catch ex As ThreadAbortException             Console.WriteLine("ThreadAbortException caught")             Thread.ResetAbort()         End Try         Console.WriteLine("Look where we are now!")         Thread.Sleep(10000)     End Sub     Public Sub Main()         Dim workerThread As New Thread(AddressOf Worker)         workerThread.Start()         Thread.Sleep(1000)         Console.WriteLine("Calling abort")         workerThread.Abort()         Thread.Sleep(2000)         Console.WriteLine("Main done")     End Sub End Module 

When the CLR throws the THReadAbortException, within the catch block you call ResetAbort(). This cancels the Abort() and the method continues running, as you can see in Figure 7-7.

Figure 7-7. Output from Example 7-6


What's the issue here? The problem with ResetAbort() is that the code that calls Abort() won't know that the Thread has cancelled it. This can lead to unexpected (and unpredictable) behavior. If you find yourself doing something like this, you might need to redesign. Look at what you are trying to achieve and evaluate other ways to accomplish it (such as synchronization objects).

IN A NUTSHELL

ResetAbort() cancels an Abort(). This is counter to normal expectations. Avoid using it and find clearer ways to achieve your goal.

SEE ALSO

Gotcha #51, "Interrupt () kicks in only when a thread is blocked" and Gotcha #52, "ThreadAbortExceptiona hot potato."



    .NET Gotachas
    .NET Gotachas
    ISBN: N/A
    EAN: N/A
    Year: 2005
    Pages: 126

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net