Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Threads and Messages

Threads and Messages

Scheduled Pinned Locked Moved C / C++ / MFC
comquestion
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shroom
    wrote on last edited by
    #1

    If I have a worker thread (let's call it thread B) and I have a control on my main window whose WndProc() is in the main application thread (thread A), and I use SendMessage() to the control from thread B, is the message processed in thread A or in thread B? What about with PostMessage()? Jeff Sand jsand at interaccess dot com

    J Z N 3 Replies Last reply
    0
    • S Shroom

      If I have a worker thread (let's call it thread B) and I have a control on my main window whose WndProc() is in the main application thread (thread A), and I use SendMessage() to the control from thread B, is the message processed in thread A or in thread B? What about with PostMessage()? Jeff Sand jsand at interaccess dot com

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      The message is processed in A in both cases. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      S 1 Reply Last reply
      0
      • S Shroom

        If I have a worker thread (let's call it thread B) and I have a control on my main window whose WndProc() is in the main application thread (thread A), and I use SendMessage() to the control from thread B, is the message processed in thread A or in thread B? What about with PostMessage()? Jeff Sand jsand at interaccess dot com

        Z Offline
        Z Offline
        Zdenek Navratil
        wrote on last edited by
        #3

        I suggest an excellent article "Using Worker Threads" [^] on this subject to your attention. Zdenek

        1 Reply Last reply
        0
        • S Shroom

          If I have a worker thread (let's call it thread B) and I have a control on my main window whose WndProc() is in the main application thread (thread A), and I use SendMessage() to the control from thread B, is the message processed in thread A or in thread B? What about with PostMessage()? Jeff Sand jsand at interaccess dot com

          N Offline
          N Offline
          Neville Franks
          wrote on last edited by
          #4

          In A as the others have said. It is not a good idea to use SendMessage between threads as you are asking for a deadlock to come knocking on your door. Neville Franks, Author of ED for Windows. www.getsoft.com

          1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            The message is processed in A in both cases. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            S Offline
            S Offline
            Steen Krogsgaard
            wrote on last edited by
            #5

            Are you sure about that? When you SendMessage you call the WndProc directly, bypassing the pump. So SendMessage would be in the context of the worker thread, while PostMessage would be in the context of the main thread (which runs the message pump). This is also the way COM handles a call to a STA component (by posting messages instead of calling the components interfaces directly). Of course, I may be wrong ;-) Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

            J 1 Reply Last reply
            0
            • S Steen Krogsgaard

              Are you sure about that? When you SendMessage you call the WndProc directly, bypassing the pump. So SendMessage would be in the context of the worker thread, while PostMessage would be in the context of the main thread (which runs the message pump). This is also the way COM handles a call to a STA component (by posting messages instead of calling the components interfaces directly). Of course, I may be wrong ;-) Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

              J Offline
              J Offline
              Joaquin M Lopez Munoz
              wrote on last edited by
              #6

              Are you sure about that? I'm 100% sure, if you pardon my arrogance. SendMessage only bypasses the message pump if issued from the same thread that attends the message. Otherwise, the message is queued as usual and the caller thread blocks until the target thread processes the message. As a side note, using SendMessage from worker threads is likely, if no care is taken, to result in deadlocks. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              S 1 Reply Last reply
              0
              • J Joaquin M Lopez Munoz

                Are you sure about that? I'm 100% sure, if you pardon my arrogance. SendMessage only bypasses the message pump if issued from the same thread that attends the message. Otherwise, the message is queued as usual and the caller thread blocks until the target thread processes the message. As a side note, using SendMessage from worker threads is likely, if no care is taken, to result in deadlocks. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                S Offline
                S Offline
                Steen Krogsgaard
                wrote on last edited by
                #7

                Relaying facts is not arrogance! I should have read the MSDN entry on SendMessage more carefully: If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message. No doubt here. I stand corrected :-O . But at least I was right on the "I could be wrong"-part :) Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                J 1 Reply Last reply
                0
                • S Steen Krogsgaard

                  Relaying facts is not arrogance! I should have read the MSDN entry on SendMessage more carefully: If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message. No doubt here. I stand corrected :-O . But at least I was right on the "I could be wrong"-part :) Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                  J Offline
                  J Offline
                  Joaquin M Lopez Munoz
                  wrote on last edited by
                  #8

                  Well, as happens so often, 100% sureness is a hard position to maintain :) As for the thread context switch, there's still no doubt about it. What I'm not so sure is whether the message sent gets queued or not. My hunch, after re-reading the docs, is that it is not, so it surpasses all other messages currently queued in the target thread. Most of the time this is of little relevance, I guess. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                  H S 2 Replies Last reply
                  0
                  • J Joaquin M Lopez Munoz

                    Well, as happens so often, 100% sureness is a hard position to maintain :) As for the thread context switch, there's still no doubt about it. What I'm not so sure is whether the message sent gets queued or not. My hunch, after re-reading the docs, is that it is not, so it surpasses all other messages currently queued in the target thread. Most of the time this is of little relevance, I guess. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                    H Offline
                    H Offline
                    Hans Ruck
                    wrote on last edited by
                    #9

                    Joaquín M López Muñoz wrote: I'm not so sure is whether the message sent gets queued or not It doesn't (checked and 100% sure :)). rechi

                    J 1 Reply Last reply
                    0
                    • H Hans Ruck

                      Joaquín M López Muñoz wrote: I'm not so sure is whether the message sent gets queued or not It doesn't (checked and 100% sure :)). rechi

                      J Offline
                      J Offline
                      Joaquin M Lopez Munoz
                      wrote on last edited by
                      #10

                      Thanx for the info! How did you do the test (just curious)? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                      H 1 Reply Last reply
                      0
                      • J Joaquin M Lopez Munoz

                        Thanx for the info! How did you do the test (just curious)? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                        H Offline
                        H Offline
                        Hans Ruck
                        wrote on last edited by
                        #11

                        I've created a SDI application with a menu item used to start the worker. The code:

                        void CMainFrame::OnFileThread() // menu item
                        {
                        DWORD *th=new DWORD;
                        HANDLE h=::CreateThread(NULL, 0, ThreadProc_, m_hWnd,
                        0, th);

                        Sleep(3000);
                        delete th;
                        }

                        DWORD WINAPI ThreadProc_(
                        LPVOID lpParameter // thread data
                        )
                        {
                        MSG msg;

                        TRACE("in thread proc\n");
                        ::PostMessage((HWND)lpParameter, WM_DUMMY3, 0, 0); // queued
                        ::SendMessage((HWND)lpParameter, WM_DUMMY2, 0, 0); // calls Func2, a handler

                        return 0;
                        }

                        LRESULT CMainFrame::Func2(WPARAM, LPARAM) // treating WM_DUMMY2
                        {
                        MSG msg;

                        TRACE("in dummy2\n");
                        while (::GetMessage(&msg, NULL, 0, 0))
                        if (msg.message==WM_DUMMY3)
                        {
                        TRACE("found dummy3\n");
                        return 0;
                        }

                        return 0;
                        }

                        And the debug output:

                        in thread proc
                        in dummy 2
                        found dummy 3

                        Due to your obvious programming skills, it should be enough :-O. rechi

                        1 Reply Last reply
                        0
                        • J Joaquin M Lopez Munoz

                          Well, as happens so often, 100% sureness is a hard position to maintain :) As for the thread context switch, there's still no doubt about it. What I'm not so sure is whether the message sent gets queued or not. My hunch, after re-reading the docs, is that it is not, so it surpasses all other messages currently queued in the target thread. Most of the time this is of little relevance, I guess. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                          S Offline
                          S Offline
                          Steen Krogsgaard
                          wrote on last edited by
                          #12

                          Hmmm. The docs suggest that the message goes through the queue ("Messages sent between threads are processed only when the receiving thread executes message retrieval code."), but doesn't specify if it gets in front of the line or has to wait until the messages in front is processed. Where's Jeff Richter or Mike Blaszczak when you need them ;P? Anyway, it's probably of little consequence. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups