On Thu, 26 Aug 2004 15:19:35 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
Lucvdv,
Windows Forms itself is displaying the Message Box, as the Load Event had an
unhandled exception.
Thanks for the information, but it still isn't clear to me.
Thread.Abort() causes a ThreadAbortException in the thread it's called on,
but not in the thread where it is called from -- so there was no unhandled
exception in the Load event.
The debug output at the bottom of this message confirms this.
In the mean time I solved my problem by just adding an IF that throws out
all ThreadAbortExceptions, but I'd still like to know why it's happening.
I was expecting that only those exceptions the debugger would normally
catch or that would make an error dialog pop up in the compiled program,
would be reported through the UnhandledException event.
It seems completely illogical that the same exception would ever be handled
twice, but that's exactly what is happening here.
In my example code, when there's no unhandled exception handler, nothing is
reported by the debugger (just comment out the AddHandler line, and the
program runs without error).
If there is such a handler, the ThreadAbortException is *first* caught by
the Catch clause in the thread, and *then* still reported as unhandled (see
output below).
I created a new console application and slightly modified the previous code
so I could see where the exception is handled first, and where it's
reported to have occurred:
'-----------------
Imports System.Threading
Module Module1
Dim thr As Thread
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, _
AddressOf UnhandledExceptionHandler
thr = New Thread(AddressOf ThreadProc)
thr.Start()
Thread.Sleep(1000)
thr.Abort()
End Sub
Private Sub UnhandledExceptionHandler(ByVal sender As Object, _
ByVal args As UnhandledExceptionEventArgs)
Dim ex As ThreadAbortException = CType(args.ExceptionObject,
ThreadAbortException)
Debug.WriteLine("Unhandled " & ex.ToString())
System.Console.WriteLine("Unhandled " & ex.ToString())
End Sub
Private Sub ThreadProc()
Try
Do
Thread.Sleep(100)
Loop
Catch ex As ThreadAbortException
Debug.WriteLine("Handled " & ex.ToString)
System.Console.WriteLine("Handled " & ex.ToString())
End Try
End Sub
End Module
'-----------------
The output, either in the debug pane or at the console in the compiled
program, shows that the exception is handled twice:
Handled System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
at ThreadAbortExceptionTest2.Module1.ThreadProc()
in D:\My Programs\Test\ThreadAbortExceptionTest2\Module1.vb :line 28
Unhandled System.Threading.ThreadAbortException: Thread was being aborted.
at ThreadAbortExceptionTest2.Module1.ThreadProc()
in D:\My Programs\Test\ThreadAbortExceptionTest2\Module1.vb :line 32
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExceptionTest2.exe' has exited with code 0
(0x0).
If you comment out line 32 (System.Console.Writeline), the 'unhandled'
message changes to line 31 (Debug.Writeline). Comment that out too, and it
changes to line 30 (Catch).
Comment out the AddHandler, and it becomes:
Handled System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
at ThreadAbortExceptionTest2.Module1.ThreadProc()
in D:\My Programs\Test\ThreadAbortExceptionTest2\Module1.vb :line 28
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExceptionTest2.exe' has exited with code 0
(0x0).
without any unhandled exception.