Click to See Complete Forum and Search --> : posting messages from child to parent


nappaji
January 25th, 2008, 05:12 PM
I have a program that spins off 2 threads and then waits for them to complete.
AS you see in the code below, I wait using the MsgWaitForMultipleObjects function.

In my ThreadFunc, I create a window and draw some primitives. It has its message handler just handles the WM_DESTROY message.
When I close the window, I want the child thread to post a message to the parent thread. I dont have a Window created in the parent. How can I do this?

For ex: Can I repost the WM_DESTROY message from the Child threads's Message handler to the parent?



INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
DWORD *pdwThreadIds, dwThrdParam = 1;
HANDLE *phThreads;
UINT uiNumThreads=2;

pdwThreadIds = new DWORD [uiNumThreads];
phThreads = new HANDLE [uiNumThreads];

for (UINT uiCnt=0; uiCnt < uiNumThreads; uiCnt++)
{
phThreads[uiCnt] = CreateThread(
NULL, 0, (LPTHREAD_START_ROUTINE)ThreadFunc , &dwThrdParam,
0,
&pdwThreadIds[uiCnt]);

if (phThreads[uiCnt] == NULL)
{
printf( "CreateThread failed (%d)\n", GetLastError() );
exit(0);
}
}

// The message loop lasts until we get a WM_QUIT message,
// upon which we shall return from the function.
UINT uiNum=uiNumThreads;
MSG msg ;
while (uiNum)
{
// block-local variable
DWORD result ;

// Read all of the messages in this next loop,
// removing each message as we read it.
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// If it is a quit message, exit.
if (msg.message == WM_QUIT)
return 1;
// Otherwise, dispatch the message.
DispatchMessage(&msg);
} // End of PeekMessage while loop.

// Wait for any message sent or posted to this queue
// or for one of the passed handles be set to signaled.
result = MsgWaitForMultipleObjects(uiNum, phThreads,
FALSE, INFINITE, QS_ALLINPUT);

// The result tells us the type of event we have.
if (result == (WAIT_OBJECT_0 + uiNumThreads))
{
// New messages have arrived.
// Continue to the top of the always while loop to
// dispatch them and resume waiting.
continue;
}
else
{
// One of the handles became signaled.
//DoStuff (result - WAIT_OBJECT_0) ;
UINT temp = result - WAIT_OBJECT_0;
CloseHandle( phThreads[temp] );
uiNum-=1;

} // End of else clause.
} // End of the always while loop.

}