Firing an event from a worker thread
Hi,
I have an ActiveX dll which implements an Event that is fired from a
worker thread. It all works well, however when the application closes
it sometimes hangs. The problem is obvious when there are many events
being fired at the time of closing.
I have traced the problem to the worker thread which is trying to fire
an event when the application is closing.
In the event I have the following lines of code, and the deadlock
occurs after the Lock();
pT->Lock();
CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);
pT->Unlock();
CComQIPtr< IDispatch, &IID_IDispatch > pDispatch( sp.p );
I have tried to use FinalRelease on the object to stop the event from
being fired, but this appears to be fired after the deadlock.
In the worker thread I have some code like
CApplicationLayer::ThreadProc(LPVOID lpParameter)
{
pApplicationLayer = reinterpret_cast< CApplicationLayer*
(lpParameter);
CoInitializeEx(NULL, COINIT_MULTITHREADED);
while (message_on_stack)
{
Fire_Event();
}
CoUninitialize();
return 0;
}
How can I stop my worker thread from sending an event as soon as the
application is closing? Should I be looking for a WM_CLOSE message
before the event is fired?
Regards,
Chirs