How to handle an exception of type ThreadAbortException -- pleasehelp!!

A

almurph

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 *****
 
J

Jason Newell

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 Newell
www.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);
}
}
}
}
 
A

almurph

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);
             }
         }
     }



}
   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.

**** 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.
 
L

Luc E. Mistiaen

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
 
A

andres castrillo

Just check this out. it worked for me
http://www.codeproject.com/Tips/128893/Why-not-to-pass-endResponse-parameter-as-true-in-R.aspx
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 do not 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 *****
On Monday, May 24, 2010 4:05 PM Jason Newell wrote:
I was just working on the same problem. From what I have gathered, you
do not. Each thread has to contain it is 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 Newell
www.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);
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top