CWorkerThread and IWorkerThreadClient not working in my ATL COM server code

Tech-Archive recommends: Fix windows errors by optimizing your registry



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 ?
.



Relevant Pages

  • Re: Multithreading and applicantion hangs
    ... That way the worker won't block waiting for the ... I am developing a Multithreaded Server based Task Scheduler wich runs ... class called Class2 which contains the information of each schedule. ... Now on each worker thread, when timer elasped it informs the main ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Atomic Operation to Set/Clear two events?
    ... Yes, manual reset, just one worker. ... The cancel routine is the only one that doesn't know if the worker has been released or not so it has to use a method that ensures the cancel is ack before it closes the event handles. ... It appears that you have a worker thread (How many worker thread do you ... I assume that A and B are manual-reset events). ...
    (microsoft.public.win32.programmer.kernel)
  • Re: c# newbie needs help with backgroundworker
    ... The most straightforward way is to use the RunWorkerAsyncoverload that allows you to pass an argument to your DoWork event handler. ... In particular, while you may be able to resolve the business of retrieving the Handle property value in the worker thread, whether you can safely use that value anywhere else is still open to question. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: UI Threat
    ... > 1) Put your worker thread in a class. ... > 2) Have a public Stats struct or class on the worker as Property. ... post that and turn off form timer. ...
    (microsoft.public.dotnet.framework)