We have a .NET 3.5 assembly (dll) being executed by a VB6 "Agent" (exe) via COM interfaces. The VB6 code does call:
' Ensure that no system dialog comes up when we GPF.
PreviousErrorMode = SetErrorMode(SEM_NOGPFAULTERRORBOX)
SetErrorMode PreviousErrorMode Or SEM_NOGPFAULTERRORBOX
' Assign our Exception Handler for this process.
PreviousExceptionFilter = SetUnhandledExceptionFilter(AddressOf UnhandledExceptionFilter)
And the VB6 UnhandledExceptionFilter
converts exceptions into VB6 raised errors. (But that's not being called here either, and this VB6 code hasn't been changed -- or even recompiled -- for years.)
In the constructor of the first .NET object created by the VB6 code, I add a handler to AppDomain.CurrentDomain.UnhandledException
(and, in the attempt to debug this problem, I've added one for Windows.Forms.Application.ThreadException
, but it didn't help, not that we have any UI displayed by .NET anyway).
I'm sure the UnhandledException
event has caught stuff when developing this code in VS2008, but I'm not sure I've seen it yet in VS2015.
The code is running multiple threads, some of which are performing the main purpose, which involves inserting and updating SQL Server data, and also communicating over a TCP protocol, which is handled by other threads, including a BackgroundWorker
.
Those performing the main purpose are created with:
taskThread = New Thread(AddressOf EnsureTaskCompletes)
taskThread.SetApartmentState(Threading.ApartmentState.STA)
taskThread.Start()
It's too proprietary and complex to show you any more code, but I have adjusted the code while debugging this issue to simply Throw New Exception
instead of the complex code that had the same effect in the end.(*)
This "simple" exception was not processed by the UnhandledExceptionHandler
and would have just silently terminated the program except that I have turned on unmanaged debugging.
Is there a documented change to the behaviour of AppDomain.CurrentDomain.UnhandledException
when the .NET Framework is "only" loaded as a dll?
(Because of the COM reregistration, I don't want to build a previous version in VS2008 yet to confirm there is a behaviour change. I have confirmed a .NET 3.5 Exe compiled by VS2015 that creates a Thread
that throws an unhandled exception does process the exception in the UnhandledException
event.)
The workaround for me is to add a Catch
to an existing Try Finally
in EnusreTaskCompletes
that already attempted to at least log that the thread was terminated before the task was complete, and call the UnhandledExceptionHandler
myself.
(*The actual problem was a missing table in a database that's only used when you've run out of credit -- I didn't add that code, or the table. This threw an exception that was not caught by the UnhandledExceptionHandler
, making it rather hard to debug -- the SQL Server Profiler finally found the issue.)