CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    147

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

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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
  •  





Click Here to Expand Forum to Full Width

Featured