|
-
March 21st, 2005, 10:09 PM
#1
SendMessage or PostMessage
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.
Code:
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:
Code:
case WM_MYMESSAGE:
if (SendMyStr(ABC)) // Unfortunately, SendMyStr can only work in Main Thread
myresult= 1;
else
myresult = 0;
break;
Please advise. Thanks.
-
March 22nd, 2005, 04:09 AM
#2
Re: SendMessage or PostMessage
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...
Code:
threadfunc()
{
// Do some processing
// Post message
// Wait for event
// Reset event
// Do some more processing
}
application()
{
// React on message
// Set event
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|