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. PostMessage to a different thread's messageQueue

PostMessage to a different thread's messageQueue

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 3 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
    snir_ya
    wrote on last edited by
    #1

    Hi, In my win32 application i open a working thread using _beginThread(...). This thread preforms a certain work and when it's done it has to notify the main application thread. I'm implementing this by having the working thread to post a message (that will be processed by the main thread) and commit suicide (_endthread()). For some reason the posted messages of the working thread are not received in the other threads message proc. Does anyone have an idea? Thanks in advance. Snir Yarom.

    C 1 Reply Last reply
    0
    • S snir_ya

      Hi, In my win32 application i open a working thread using _beginThread(...). This thread preforms a certain work and when it's done it has to notify the main application thread. I'm implementing this by having the working thread to post a message (that will be processed by the main thread) and commit suicide (_endthread()). For some reason the posted messages of the working thread are not received in the other threads message proc. Does anyone have an idea? Thanks in advance. Snir Yarom.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Maybe posting the relevant code will help. BTW why doesn't the worker thread just return?

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      S 2 Replies Last reply
      0
      • C CPallini

        Maybe posting the relevant code will help. BTW why doesn't the worker thread just return?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        S Offline
        S Offline
        snir_ya
        wrote on last edited by
        #3

        Here's the relevant code: void RunAuthenticate(LPVOID pThreadId); //decleration void MyClass::Authenticate(std::string userName, std::string password, int *balance, HWND hwnd) { //working thread initialization m_threadNum++; HANDLE hThrd = (HANDLE)_beginthread(RunAuthenticate, 0, &m_threadNum); } //Global function void RunAuthenticate(LPVOID pThreadId) { HWND hwnd = CComManager::GetInstance()->GetCallingWnd(pThreadId); //return the calling window hwnd long proc; DWORD hThread = ::GetWindowThreadProcessId(hwnd,(LPDWORD)&proc); if(hThread != NULL) ::PostThreadMessage((DWORD) hThread,(UINT)THREAD_FINISHED, (WPARAM)status, NULL); } Thanks.

        1 Reply Last reply
        0
        • C CPallini

          Maybe posting the relevant code will help. BTW why doesn't the worker thread just return?

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          S Offline
          S Offline
          snir_ya
          wrote on last edited by
          #4

          More Clarifications: The calling thread returns right after initializing the working thread. The working thread should notify the main thread asynchly when his job was done. The notification message (::PostThreadMessage(...)) isn't caught in the main thread message proc. Thanks again. Snir_ya

          C 1 Reply Last reply
          0
          • S snir_ya

            More Clarifications: The calling thread returns right after initializing the working thread. The working thread should notify the main thread asynchly when his job was done. The notification message (::PostThreadMessage(...)) isn't caught in the main thread message proc. Thanks again. Snir_ya

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            What is CComManager (is your?)? Why don't you simply pass the Main Window handle to the working thread so that the latter can post a message to?

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            S 1 Reply Last reply
            0
            • C CPallini

              What is CComManager (is your?)? Why don't you simply pass the Main Window handle to the working thread so that the latter can post a message to?

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              S Offline
              S Offline
              snir_ya
              wrote on last edited by
              #6

              Thanks for your reply. 1. Yes, CComManager is mine. In my code it returns the HWND of the (login) dialog that called the working thread. The (login) dialog was intialized by the main thread. The working thread needs to post a message to this dialog. But all the messages sent by the working thread are lost. 2. It's a win32 application. How do i get the Main window handle? Thanks again for your help. Snir_ya.

              C R 2 Replies Last reply
              0
              • S snir_ya

                Thanks for your reply. 1. Yes, CComManager is mine. In my code it returns the HWND of the (login) dialog that called the working thread. The (login) dialog was intialized by the main thread. The working thread needs to post a message to this dialog. But all the messages sent by the working thread are lost. 2. It's a win32 application. How do i get the Main window handle? Thanks again for your help. Snir_ya.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Post the message to the main window. When you create the main window (i.e.e inside InitInstance in the wizard-generated code), you have the great opportunity to store its handle for later use. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                1 Reply Last reply
                0
                • S snir_ya

                  Thanks for your reply. 1. Yes, CComManager is mine. In my code it returns the HWND of the (login) dialog that called the working thread. The (login) dialog was intialized by the main thread. The working thread needs to post a message to this dialog. But all the messages sent by the working thread are lost. 2. It's a win32 application. How do i get the Main window handle? Thanks again for your help. Snir_ya.

                  R Offline
                  R Offline
                  rp_suman
                  wrote on last edited by
                  #8

                  If you use CreateWindow(), it will return window handle. Check this page: Creating Win32 Applications[^] See the point 3. Best Regards, Suman

                  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