CWorkerThread and IWorkerThreadClient not working in my ATL COM server
code
I am using CWorkerThread and IWorkerThreadClient in my ATL code but they
do not seem to be working in conjunction with
MsgWaitForMultipleObjectsEx. My code looks like:
// In the main thread:
HANDLE workerThreadHandle;
workerThread = new CWorkerThread<>;
workerThreadImpl = new ProcessThread;
ProcessingData * data = new ProcessingData;
// Code to initialize processing data ...
workerThread -> Initialize();
workerThread ->
AddTimer(1,workerThreadImpl,reinterpret_cast<DWORD_PTR>(data),&workerThreadHandle);
while (true)
{
DWORD
res(::MsgWaitForMultipleObjectsEx(1,&workerThreadHandle,INFINITE,QS_ALLINPUT,0));
if (res == WAIT_OBJECT_0)
{
// Thread has ended (1)
break;
}
else
{
MSG msg;
while (true)
{
if (::PeekMessage(&msg,0,0,0,PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else
{
break;
}
}
}
}
// Further processing...
In the worker thread:
HRESULT ProcessThread::Execute(DWORD_PTR dwParam,HANDLE hObject)
{
CoInitializeEx(0,COINIT_APARTMENTTHREADED);
ProcessingData * data = reinterpret_cast<ProcessingData *>(dwParam);
// Lots of processing here ...
CoUninitialize();
return S_OK;
}
In the code above, the 'break' in the main 'while (true)' loop of my
main thread, shown at (1), is occuring before the Execute of my worker
thread is finished.
Am I missing something or is the worker thread not working like it
should in ATL ?