Re: Firing an event from a worker thread
Attach a debugger after the deadlock and examine all the
threads. That should tell you for sure what is deadlocking
without the need for you to put debug statements.
My preliminary guess is you are blocking (via wait call)
the STA trying to stop your worker thread while it still
has sinks to dispatch events to. It tries to call into the STA
and blocks, but the call can't make it through since the STA
is blocked and can't process windows messages, hence
your deadlock. Check out AtlWaitWithMessageLoop and
CoWaitForMultipleHandles (Win2K and later). Beware
you are introducing reentrancy here (not sure if you'd need
to be concerned about it, just pointing out the fact...).
Microsoft MVP [VC], MCSD
MVP VC FAQ: http://www.mvps.org/vcfaq
"Chris" <firstname.lastname@example.org> wrote in message
Thanks for your reply!
I can't stop the app before the deadlock. I put in some more debug
statements, and the last call before the deadlock is
GetInterfaceFromGlobal from within the CComDynamicUnkArray_GIT template
class. The Worker thread is making this call.
I built a test application to demonstrate the issue, and I can send you
a copy of it if you are interested. I have a simple ActiveX dll and a
The only solution that I can come up with is to use QueryUnload from VB
to stop the worker thread before allowing the application to close. If
I do this, then it closes gracefully all of the time. QueryUnload is
fired before FinalRelease, and if the worker thread is running I set
Cancel to True to prevent the form from unloading. A timer is fired 10
ms later to unload the form and by this time the worker thread has
stopped and the application once again calls QueryUnload with Cancel
set to false to allow the form to unload.
It's an ugly work around, but I don't have a better solution as yet.