473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

BackgroundThread does not get ThreadAbortException

My background thread is being terminated when my application exixts.
However, it does not receive a ThreadAbortException so I am unable to
cleanly diconnect from a remote object (which makes the remote object fail).
The thread does get aborted, just no exception is raised.

If I explicitly call the thread.Abort() method, the exception is raised. Is
that by design that background threads will not catch the ThreadAbort
exception when the app is exiting?

Thanks
Mitch
Nov 16 '05 #1
6 2810
Hi Mitch,

I am not sure on this but it might be that app is killing the threads in a
different way.
How are you exiting the app?
Anyway, you should really kill those threads manually before the app is
closed.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Mitch" <xx***@avantium.com> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...
My background thread is being terminated when my application exixts.
However, it does not receive a ThreadAbortException so I am unable to
cleanly diconnect from a remote object (which makes the remote object fail). The thread does get aborted, just no exception is raised.

If I explicitly call the thread.Abort() method, the exception is raised. Is that by design that background threads will not catch the ThreadAbort
exception when the app is exiting?

Thanks
Mitch

Nov 16 '05 #2

"Mitch" <xx***@avantium.com> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...
My background thread is being terminated when my application exixts.
However, it does not receive a ThreadAbortException so I am unable to
cleanly diconnect from a remote object (which makes the remote object
fail).
The thread does get aborted, just no exception is raised.

If I explicitly call the thread.Abort() method, the exception is raised.
Is
that by design that background threads will not catch the ThreadAbort
exception when the app is exiting?

Thanks
Mitch


When you call Application.Exit the CLR calls the "ExitProcess" kernel
service, this service simply kills all threads (except the calling thread)
in the process with no chance to run additional code like exception
handlers.
So it's up to the application to clean up it's resources when exiting. This
can be done by implementing the ApplicationExit event handler, however you
should try to avoid using asynchronous thread aborts and use synchronization
primitives to signal thread abortion requests.

Willy.
Nov 16 '05 #3
This behavior is not well documented but is expected. The problem is that
what the runtime considers to be a graceful shutdown is not the same thing
that applications would consider to be a graceful shutdown. Shutting down a
process is not at all the same as unloading an appdomain, which it sounds
like you are expecting.

The basic sequence the runtime follows at process shutdown (with a lot left
out) is...

1. Starts a watchdog thread - if it times out the shutdown process is halted
and the runtime calls ExitProcess which immediately terminates the app.
2. The finalizer thread finalizes all unreachable objects - this is a normal
sweep.
3. The ProcessExit is raised (only the default appdomain gets this event).
4. Suspend all managed threads. The threads are never resumed, so code in
finally blocks will not run.
5. Finalize all objects, including reachable ones.
6. Lots of other shutdown activities occur, but none that bear on your
problem.
7. The process exits.

This sequence is very different from that of unloading an appdomain.

Note that a ThreadAbort is not raised, so manual threads will not receive
any notification from the runtime that the process is exiting. If you need a
notification you will need to write your own process shutdown code that will
cause all your manual threads to exit. You could hook the ProcessExit event
and use that to unload all appdomains and raise ThreadAbortExceptions in
your threads, or use some other means of signalling the threads.

I generally try to avoid using the ThreadAbortException due to side-effects
as you could interrupt code running in a finally block. I prefer to signal a
manual event that the worker threads block on. This allows them to finish
work in progress and then exit in a synchronous manner.

This is explained in great detail at:
http://blogs.msdn.com/cbrumme/archiv.../20/51504.aspx

Dave

"Mitch" <xx***@avantium.com> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...
My background thread is being terminated when my application exixts.
However, it does not receive a ThreadAbortException so I am unable to
cleanly diconnect from a remote object (which makes the remote object fail). The thread does get aborted, just no exception is raised.

If I explicitly call the thread.Abort() method, the exception is raised. Is that by design that background threads will not catch the ThreadAbort
exception when the app is exiting?

Thanks
Mitch

Nov 16 '05 #4
>
When you call Application.Exit the CLR calls the "ExitProcess" kernel
service, this service simply kills all threads (except the calling thread)
in the process with no chance to run additional code like exception
handlers.


small point: finalizers still run. See my other reply to this post.

Dave


Nov 16 '05 #5
Thanks for the replies. One complication is that the main application
doesn't really know about my code, since my code is a "drop in" that is
discovered at run time. Since my code is independent of the main app, I
hooked up to an exit event:

AppDomain.CurrentDomain.ProcessExit +=new
EventHandler(CurrentDomain_ProcessExit);

From this event handler I go through an arraylist I've kept of my threads,
to exit each one individually.

Thanks
Mitch

"Dave" <no****************@wi.rr.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
This behavior is not well documented but is expected. The problem is that
what the runtime considers to be a graceful shutdown is not the same thing
that applications would consider to be a graceful shutdown. Shutting down a process is not at all the same as unloading an appdomain, which it sounds
like you are expecting.

The basic sequence the runtime follows at process shutdown (with a lot left out) is...

1. Starts a watchdog thread - if it times out the shutdown process is halted and the runtime calls ExitProcess which immediately terminates the app.
2. The finalizer thread finalizes all unreachable objects - this is a normal sweep.
3. The ProcessExit is raised (only the default appdomain gets this event).
4. Suspend all managed threads. The threads are never resumed, so code in
finally blocks will not run.
5. Finalize all objects, including reachable ones.
6. Lots of other shutdown activities occur, but none that bear on your
problem.
7. The process exits.

This sequence is very different from that of unloading an appdomain.

Note that a ThreadAbort is not raised, so manual threads will not receive
any notification from the runtime that the process is exiting. If you need a notification you will need to write your own process shutdown code that will cause all your manual threads to exit. You could hook the ProcessExit event and use that to unload all appdomains and raise ThreadAbortExceptions in
your threads, or use some other means of signalling the threads.

I generally try to avoid using the ThreadAbortException due to side-effects as you could interrupt code running in a finally block. I prefer to signal a manual event that the worker threads block on. This allows them to finish
work in progress and then exit in a synchronous manner.

This is explained in great detail at:
http://blogs.msdn.com/cbrumme/archiv.../20/51504.aspx

Dave

"Mitch" <xx***@avantium.com> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...
My background thread is being terminated when my application exixts.
However, it does not receive a ThreadAbortException so I am unable to
cleanly diconnect from a remote object (which makes the remote object

fail).
The thread does get aborted, just no exception is raised.

If I explicitly call the thread.Abort() method, the exception is raised.

Is
that by design that background threads will not catch the ThreadAbort
exception when the app is exiting?

Thanks
Mitch


Nov 16 '05 #6

"Mitch" <xx***@avantium.com> wrote in message
news:On**************@TK2MSFTNGP09.phx.gbl...
Thanks for the replies. One complication is that the main application
doesn't really know about my code, since my code is a "drop in" that is
discovered at run time. Since my code is independent of the main app, I
hooked up to an exit event:

AppDomain.CurrentDomain.ProcessExit +=new
EventHandler(CurrentDomain_ProcessExit);

From this event handler I go through an arraylist I've kept of my threads,
to exit each one individually.

Thanks
Mitch

That seems reasonable and it ought to work.

Dave


Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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 (ThreadAbortedException occurs). The log file gets stuck open until...
6
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 diconnect from a remote object (which makes the...
4
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 ThreadAbortException's behavior designed to be this way? ...
6
by: David Waz... | last post by:
Moved an app from W/2000 Asp V1.0 to W/2003, VS/2003, ASPV 1.1 Page runs a long job, uploading 2 large fixed length files (300,000 rows) into SQL database. A process is run against the data,...
4
by: splap20 | last post by:
Every day a large chunk of XML is posted up to my app via http. The app interprets this XML and writes results to a SQLServer. Occasionally a ThreadAbortException is thrown during this process....
1
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 ThreadAbortException, but it was not staying caught and was showing...
1
by: Burak Gunay | last post by:
Hello, I have a piece of code where I am writing the contents of a datagrid to an excel report Dim tw As New StringWriter() Dim hw As New System.Web.UI.HtmlTextWriter(tw) Dim frm As HtmlForm...
3
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 current problem. I am working on the current issue,...
5
by: dorrit.Riemenschneider | last post by:
I've developed a sharepint webpart (ASP.NET 2.0 application) with use of the CrystalReportViewer web control (CrystalDecisions.Web.CrystalReportViewer). All works fine except the export of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.