B
In der MSDN ist ein Beispiel drin ...
Hier die Kurzfassung:
Empfangs-Thread mit Message-Queue erstellen:
...
// create an event for the thread's queue completition
hEvent = CreateEvent (NULL, false, true, QEVENT);
hThread = CreateThread (
NULL, // no security attributes
0, // use default stack size
Receive_Thread, // thread function
&stThread, // argument to thread function
0, // use default creation flags
&idThread); // returns the thread identifier
// wait for queue completition
DWORD dwState = WaitForSingleObject (hEvent, 100); // 100 ms warten
...
Empfangs-Thread:
DWORD WINAPI Receive_Thread (LPVOID lpParam)
{
...
// create a queue for the thread
PeekMessage ((LPMSG)&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
// sent event after queue creation back to the threads creator
SetEvent (hEvent);
// get messages by message from threads local queue
while ((bRet = GetMessage (&msg, NULL, 0, 0)) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
return 1; // error!
}
else
{ // a valid message is received
switch (msg.message)
{
case QS_TIMER: // a WM_TIMER message is in the queue (SetTimer)
.... // tu' irgendwas (lParam, wParam auswerten ...)
break;
default:
.... // tu' irgendwas
break;
} // end of: switch (msg.message)
} // end of: if (bRet == -1) else ...
} // end of: while((bRet = GetMessage(...
...
Aufruf von PostThreadMessage () von irgendwo:
...
BOOL bRet = PostThreadMessage (idThread, Msg, wParam, lParam);
...
Blackbird