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