From: almurph on
Hi,


I am trying to answer the question of how do you handle an exception
of type "ThreadAbortException". I think that it can be caught by a
catch and then use the Thread.ResetAbort() method call. I wrote a
program to prove this to myself but it does not seem to be catching it
in the "ThreadAbortException" part of the catch but this is not
happening. The finally is called - that is all.
As I am kind of new to threads I don't know if I did something wrong
in the code, or if my understanding is wrong. Either way, I would
appreciate your comments on my little program below.

Thanks,
Al.


**** BEGIN CODE ****


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Scratch
{
class Threading
{
public static void Startprocess()
{

Thread calc1T = new Thread(new ThreadStart(LongCalc1));

try
{
Console.WriteLine(calc1T.ThreadState.ToString());
calc1T.Start();
Console.WriteLine(calc1T.ThreadState.ToString());
calc1T.Abort();
Console.WriteLine(calc1T.ThreadState.ToString());
}
catch (ThreadAbortException e)
{
Console.WriteLine(e.ToString());
Thread.ResetAbort();
}
finally
{
Console.WriteLine("Finally reached");
Console.WriteLine(calc1T.ThreadState.ToString());
}

Console.WriteLine("After finally");

}


public static void LongCalc1()
{
long ans = 0;
for (long i = 0; i < 1000000000; i++)
{
ans += i;
}
Console.WriteLine("Answer is: {0}", ans.ToString());
}
}
}


***** END CODE *****


From: almurph on
On May 24, 9:05 pm, Jason Newell <nos...(a)nospam.com> wrote:
> I was just working on the same problem.  From what I've gathered, you
> don't.  Each thread has to contain it's on try\catch and deal with the
> exception internally.  Below is a demo that I wrote for myself reading
> from examples in various sources.
>
> Jason Newellwww.jasonnewell.net
>
> using System;
> using System.Collections.Generic;
> using System.Linq;
> using System.Text;
> using System.Threading;
>
> namespace ThreadTest
> {
>      class Program
>      {
>          static volatile bool _shouldStop = false;
>
>          static void Main(string[] args)
>          {
>              Thread thread1 = new Thread(new ThreadStart(ThreadTest1));
>              Thread thread2 = new Thread(new ThreadStart(ThreadTest2));
>
>              thread1.Start();
>              thread2.Start();
>
>              thread1.Join();
>              thread2.Join();
>
>              /* Show that our thread tests are complete */
>              for (int i = 0; i < 5; i++)
>              {
>                  Thread.Sleep(1000);
>                  Console.WriteLine("Main - {0}", DateTime.Now.ToString());
>              }
>          }
>
>          static void ThreadTest1()
>          {
>              try
>              {
>                  for (int i = 0; i < 1000; i++)
>                  {
>                      if (_shouldStop) return;
>                      Thread.Sleep(1000);
>                      Console.WriteLine("ThreadTest1 - {0}",
> DateTime.Now.ToString());
>
>                      /* Force an exception */
>                      if (i == 5)
>                      {
>                          throw new System.Exception("Test exception");
>                      }
>                  }
>              }
>              catch (System.Exception ex)
>              {
>                  _shouldStop = true;
>                  Console.WriteLine("ThreadTest1 - {0}", ex.Message);
>              }
>          }
>
>          static void ThreadTest2()
>          {
>              try
>              {
>                  for (int i = 0; i < 1000; i++)
>                  {
>                      if (_shouldStop) return;
>                      Thread.Sleep(1000);
>                      Console.WriteLine("ThreadTest2 - {0}",
> DateTime.Now.ToString());
>                  }
>              }
>              catch (System.Exception ex)
>              {
>                  _shouldStop = true;
>                  Console.WriteLine("ThreadTest2 - {0}", ex.Message);
>              }
>          }
>      }
>
>
>
> }
> almu...(a)altavista.com wrote:
> > Hi,
>
> >    I am trying to answer the question of how do you handle an exception
> > of type "ThreadAbortException". I think that it can be caught by a
> > catch and then use the Thread.ResetAbort() method call. I wrote a
> > program to prove this to myself but it does not seem to be catching it
> > in the "ThreadAbortException" part  of the catch but this is not
> > happening. The finally is called - that is all.
> >    As I am kind of new to threads I don't know if I did something wrong
> > in  the code, or if my understanding is wrong. Either way, I would
> > appreciate your comments on my little program below.
>
> > Thanks,
> > Al.
>
> > **** BEGIN CODE ****
>
> > using System;
> > using System.Collections.Generic;
> > using System.Linq;
> > using System.Text;
> > using System.Threading;
>
> > namespace Scratch
> > {
> >     class Threading
> >     {
> >         public static void Startprocess()
> >         {
>
> >             Thread calc1T = new Thread(new ThreadStart(LongCalc1));
>
> >             try
> >             {
> >                 Console.WriteLine(calc1T.ThreadState.ToString());
> >                 calc1T.Start();
> >                 Console.WriteLine(calc1T.ThreadState.ToString());
> >                 calc1T.Abort();
> >                 Console.WriteLine(calc1T.ThreadState.ToString());
> >             }
> >             catch (ThreadAbortException e)
> >             {
> >                 Console.WriteLine(e.ToString());
> >                 Thread.ResetAbort();
> >             }
> >             finally
> >             {
> >                 Console.WriteLine("Finally reached");
> >                 Console.WriteLine(calc1T.ThreadState.ToString());
> >             }
>
> >             Console.WriteLine("After finally");
>
> >         }
>
> >         public static void LongCalc1()
> >         {
> >             long ans = 0;
> >             for (long i = 0; i < 1000000000; i++)
> >             {
> >                 ans += i;
> >             }
> >             Console.WriteLine("Answer is: {0}", ans.ToString());
> >         }
> >     }
> > }
>
> > ***** END CODE *****- Hide quoted text -
>
> - Show quoted text -

Hi Jason,

Thansk fo r the example but I don't see Abort() method call
anywhere in your code (so I don;t think this addresses what I am
trying to get at) - and while I am not against exceptions of type
System.Exception - you lose granularity on the exact nature of the
exception if you have to parse logs.
My questiosn still holds - why can I not catch the
ThreadAbortException?

Confused,
Al.
From: Luc E. Mistiaen on
> My questiosn still holds - why can I not catch the
> ThreadAbortException?

You can, but the exception handler must be in the thread aborted itself, not
in the thread that does the abort (if it is different). This was what the
previous poster was showing event if it didn't abort the thread
explicitely...

/LM