If a childThread is in the middle of a catch block and handling an
exception caught, the main thread calls childThread.Abort(). At that
time a ThreadAbortException is thrown in the childThread. The question
is: after the ThreadAbortException is thrown, does the childThread
continue run the remaining code in the catch block? 6 7292
If a childThread is in the middle of a catch block and handling an
exception caught, the main thread calls childThread.Abort(). At that
time a ThreadAbortException is thrown in the childThread. The
question is: after the ThreadAbortException is thrown, does the
childThread continue run the remaining code in the catch block?
I don't think the code in the child thread's catch block will continue to
run. The documentation for Thread.Abort() mentions that, in .NET Framework
version 2.0, code in finally blocks will execute but it does not mention
catch blocks. You could test this be signalling the thread that aborts the
child thread from the child thread's catch block and then performing the
Abort and determining if the code after the signal is executed.
Best Regards,
Dustin Campbell
Developer Express Inc.
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.
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
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?
Bill, if you read my response carefully and run the test app, you'll find
that the remainder of the catch-block does run when a ThreadAbortException
occurs. If it didn't, this line would not have been sent to the console:
"ThreadState at end of catch-block: AbortRequested".
Best Regards,
Dustin Campbell
Developer Express Inc.
"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
Bill, if you read my response carefully and run the test app, you'll find
that the remainder of the catch-block does run when a ThreadAbortException
occurs. If it didn't, this line would not have been sent to the console:
"ThreadState at end of catch-block: AbortRequested".
That answers the question of what *does* happen. It doesn't answer the
question of what *should* happen.
I think Bill was asking the latter. I have to admit, I find it
non-intuitive that a ThreadAbortException doesn't get thrown immediately,
even when in the middle of a catch block. After all, as far as I know any
other exception that occurs while within the catch block gets thrown when it
happens.
It's definitely good to know what does happen. But it's also interesting to
know what should happen, especially when that's different from what does
happen. :)
Pete
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.
<fo*******@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
|
| 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
| This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Anushya |
last post by:
Hi
When i abort a thread i get threadabort exception and wot all the
things that need to be done there? IS releasing all the objects that
are...
|
by: Mark Phillips |
last post by:
Hello,
I am having a problem in which a file can get stuck open when a thread
that is attempting to write to it gets aborted...
|
by: Bonj |
last post by:
Say if i have a function,
void FuncStart()
{
try
{
FuncDoActions();
//<nextlineofcode>
}
catch(ThreadAbortException)
|
by: Mitch |
last post by:
My background thread is being terminated when my application exixts.
However, it does not receive a ThreadAbortException so I am unable to
cleanly...
|
by: Vivek |
last post by:
Hi,
I have a question regarding ThreadAbortException. Why is the thread abort
exception rethrown at the end of a catch clause?
Why is...
|
by: Steve - DND |
last post by:
I just implemented a page that performs a Response.Redirect(url, true). As
such, I wrapped it in a try catch, and explicitly caught a...
|
by: Eric |
last post by:
I have the following situation: I was getting intermittent errors using
Reponse.Redirct("url", true) and was trying to catch the...
|
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= |
last post by:
Hi,
I am running a web service which sometimes throws exceptions. I have a
lot of error trapping within the web service, but I have missed the...
|
by: dorrit.Riemenschneider |
last post by:
I've developed a sharepint webpart (ASP.NET 2.0 application) with use
of the CrystalReportViewer web control...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...
| |