BackgroundThread does not get ThreadAbortException

M

Mitch

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
 
M

Miha Markic [MVP C#]

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

Willy Denoyette [MVP]

Mitch said:
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.
 
D

Dave

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/archive/2003/08/20/51504.aspx

Dave
 
D

Dave

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
 
M

Mitch

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
 
D

Dave

Mitch said:
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
 

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