Click to See Complete Forum and Search --> : SendMessage or PostMessage


Dimension
March 21st, 2005, 09:09 PM
Hi,

I read in MSDN regarding the different between both.

Now, this is my scenario.

In my worker thread, I need to send message to Main Thread to execute something and return something to me before proceed to next statement.
Which is the best way to accomplish this ?

I'm worried that using SendMessage will cause exception, as the Thread might be blocked. Using PostMessage will return immediately without waiting for the message to process finish. If you have better approach, let me know.


DWORD WINAPI SendStr (LPVOID pParam)
{
SendMessage(My_hWnd, WM_MYMESSAGE, ...);
result = myresult;
if (result != 0)
{
// do something A
}
else
{
// do something B
}

}


in Main Thread:


case WM_MYMESSAGE:
if (SendMyStr(ABC)) // Unfortunately, SendMyStr can only work in Main Thread
myresult= 1;
else
myresult = 0;
break;



Please advise. Thanks.

Andreas Masur
March 22nd, 2005, 03:09 AM
Well....using 'SendMessage' within a thread always opens the door for deadlocks. Thus, I woud rather suggest an event....which will be set by the application to trigger the thread to continue...

threadfunc()
{
// Do some processing
// Post message
// Wait for event
// Reset event
// Do some more processing
}

application()
{
// React on message
// Set event
}