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