P
pawel.kedzior
Hi
In .NET 2.0 unhandled exceptions in separate threads cause raising
unhandled exception event and terminating the process. This is true
for most exceptions, but not for ThreadAbortException, what is
reasonable.
I have verified this with following example:
class Program {
private static void thread() {
while (true){}
}
static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException
+= new UnhandledExceptionEventHandler(handler);
Thread th = new Thread(new ThreadStart(thread))
th.Start();
Thread.Sleep(5000);
th.Abort();
Thread.Sleep(50000);
}
static void handler(object sender, UnhandledExceptionEventArgs
e) {
System.Console.WriteLine("Unhandled exception.");
}
}
While working with my, quite complex, application, where I must use
Thread.Abort() on one of my threads, I managed to get in the handler
following exception:
Exception: System.Threading.ThreadAbortException
Message: Thread was being aborted.
Source: mscorlib
at System.Reflection.Assembly.GetType(String name, Boolean
throwOnError, Boolean ignoreCase)
at System.Activator.CreateInstanceFrom(String assemblyFile, String
typeName, Boolean ignoreCase,
BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo
culture, Object[]
activationAttributes, Evidence securityInfo)
at System.AppDomain.CreateInstanceFrom(String assemblyFile, String
typeName)
The application terminated immediately because of that.
This convinced me that in some circumstances ThreadAbortException is
treated just as any other exception and can cause process crash
in .NET 2.0.
The above stack trace is a full stack trace which the exception
conveyed, and looks bit strange for me. My code never use
System.AppDomain.CreateInstanceFrom(..) directly, so I guess this is
some internal mechanism of the framwork/CLR.
Can you think what kind of my code can trigger above one ? It looks
like something related to reflection. What is exact reason that the
ThreadAbortException is not treated just as ThreadAbortException in my
example.
Generally this is very bad news for everyone who use Thread.Abort(),
because there are poor guarantees that the operation does not crash
their application.
PS. Do not suggest me using a flag to stop a thread.
In .NET 2.0 unhandled exceptions in separate threads cause raising
unhandled exception event and terminating the process. This is true
for most exceptions, but not for ThreadAbortException, what is
reasonable.
I have verified this with following example:
class Program {
private static void thread() {
while (true){}
}
static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException
+= new UnhandledExceptionEventHandler(handler);
Thread th = new Thread(new ThreadStart(thread))
th.Start();
Thread.Sleep(5000);
th.Abort();
Thread.Sleep(50000);
}
static void handler(object sender, UnhandledExceptionEventArgs
e) {
System.Console.WriteLine("Unhandled exception.");
}
}
While working with my, quite complex, application, where I must use
Thread.Abort() on one of my threads, I managed to get in the handler
following exception:
Exception: System.Threading.ThreadAbortException
Message: Thread was being aborted.
Source: mscorlib
at System.Reflection.Assembly.GetType(String name, Boolean
throwOnError, Boolean ignoreCase)
at System.Activator.CreateInstanceFrom(String assemblyFile, String
typeName, Boolean ignoreCase,
BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo
culture, Object[]
activationAttributes, Evidence securityInfo)
at System.AppDomain.CreateInstanceFrom(String assemblyFile, String
typeName)
The application terminated immediately because of that.
This convinced me that in some circumstances ThreadAbortException is
treated just as any other exception and can cause process crash
in .NET 2.0.
The above stack trace is a full stack trace which the exception
conveyed, and looks bit strange for me. My code never use
System.AppDomain.CreateInstanceFrom(..) directly, so I guess this is
some internal mechanism of the framwork/CLR.
Can you think what kind of my code can trigger above one ? It looks
like something related to reflection. What is exact reason that the
ThreadAbortException is not treated just as ThreadAbortException in my
example.
Generally this is very bad news for everyone who use Thread.Abort(),
because there are poor guarantees that the operation does not crash
their application.
PS. Do not suggest me using a flag to stop a thread.