Patrik Löwendahl said:
Are you sure about that?
All tests I've ever done never indicates a ThreadAbortException is
injected when the application domain is unloading. Isn't CLR just
killing them?
According to the docs for AppDomain.Unload:
<quote>
The threads in domain are terminated using the Abort method, which
throws the thread an instance of ThreadAbortException. Although the
thread should terminate promptly, it can continue executing for an
unpredictable amount of time in its finally clause.
</quote>
Here's a complete pair of programs to demonstrate it:
Test.cs - compile to test.exe
using System;
class Test
{
static void Main()
{
AppDomain dom = AppDomain.CreateDomain ("Foo");
dom.ExecuteAssembly ("test2.exe");
// By the time this returns, the other thread will be running
AppDomain.Unload(dom);
}
}
Test2.cs - compile to test2.exe
using System;
using System.Threading;
class Test2
{
static void Main()
{
new Thread (new ThreadStart(ThreadJob)).Start();
Thread.Sleep(100);
}
static void ThreadJob()
{
Console.WriteLine ("Executing in other app domain thread");
try
{
Thread.Sleep(2000);
}
catch (Exception e)
{
Console.WriteLine ("Got exception "+e);
}
}
}
Results:
Executing in other app domain thread
Got exception System.Threading.ThreadAbortException: Thread was being
aborted.
at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
at Test2.ThreadJob()