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. SendMessage(MainWnd) from another thread

SendMessage(MainWnd) from another thread

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
5 Posts 3 Posters 1 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.
  • H Offline
    H Offline
    Hosam Aly Mahmoud
    wrote on last edited by
    #1

    I have an API program in which I have one thread other than the main thread. In the other thread, I wrote MessageBox(globalMainWndHWND, ...), trying to get a message under my main window. The problem is that at this step the MessageBox function does not return! I thought that Windows might prevent threads from using objects created by other threads. I tried to pass the HWND as a parameter to the thread function (instead of using the global variable), but the same problem happened again. SendMessage did the same thing. Could someone please tell me how I could solve this problem? Thank you very much.

    Hosam Aly Mahmoud

    D M 2 Replies Last reply
    0
    • H Hosam Aly Mahmoud

      I have an API program in which I have one thread other than the main thread. In the other thread, I wrote MessageBox(globalMainWndHWND, ...), trying to get a message under my main window. The problem is that at this step the MessageBox function does not return! I thought that Windows might prevent threads from using objects created by other threads. I tried to pass the HWND as a parameter to the thread function (instead of using the global variable), but the same problem happened again. SendMessage did the same thing. Could someone please tell me how I could solve this problem? Thank you very much.

      Hosam Aly Mahmoud

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      If the secondary thread does not have its own message pump (i.e., worker thread), calling MessageBox() in it will effectively block the message pump in the primary thread.


      Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

      H 1 Reply Last reply
      0
      • H Hosam Aly Mahmoud

        I have an API program in which I have one thread other than the main thread. In the other thread, I wrote MessageBox(globalMainWndHWND, ...), trying to get a message under my main window. The problem is that at this step the MessageBox function does not return! I thought that Windows might prevent threads from using objects created by other threads. I tried to pass the HWND as a parameter to the thread function (instead of using the global variable), but the same problem happened again. SendMessage did the same thing. Could someone please tell me how I could solve this problem? Thank you very much.

        Hosam Aly Mahmoud

        M Offline
        M Offline
        Mike Dimmick
        wrote on last edited by
        #3

        Can I see if I can clarify the question? At this point in the secondary thread, you want to cause a message box to be displayed, but you want your secondary thread to continue running even though there's a message box up. To do this, you'll need to post a message to your primary thread (probably to the main window) to cause it to create the message box, using PostMessage. Unlike SendMessage, PostMessage is non-blocking - it returns immediately after adding the message to the message queue of the thread that owns the window. SendMessage blocks the sending thread until the receiving thread responds, by either returning from the window procedure or by calling ReplyMessage (desktop only; Windows CE does not offer this function). The return value of SendMessage is the return value of the window procedure, or the value passed to ReplyMessage.

        H 1 Reply Last reply
        0
        • M Mike Dimmick

          Can I see if I can clarify the question? At this point in the secondary thread, you want to cause a message box to be displayed, but you want your secondary thread to continue running even though there's a message box up. To do this, you'll need to post a message to your primary thread (probably to the main window) to cause it to create the message box, using PostMessage. Unlike SendMessage, PostMessage is non-blocking - it returns immediately after adding the message to the message queue of the thread that owns the window. SendMessage blocks the sending thread until the receiving thread responds, by either returning from the window procedure or by calling ReplyMessage (desktop only; Windows CE does not offer this function). The return value of SendMessage is the return value of the window procedure, or the value passed to ReplyMessage.

          H Offline
          H Offline
          Hosam Aly Mahmoud
          wrote on last edited by
          #4

          Thanks for your help. I am sorry to say that your clarification missed a bit. I want to stop the secondary thread, and get out the message box. After the message box returns yes or no, the secondary thread will do work according to the user's choice. But thanks for your help anyway! :rose:

          Hosam Aly Mahmoud

          1 Reply Last reply
          0
          • D David Crow

            If the secondary thread does not have its own message pump (i.e., worker thread), calling MessageBox() in it will effectively block the message pump in the primary thread.


            Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

            H Offline
            H Offline
            Hosam Aly Mahmoud
            wrote on last edited by
            #5

            Thanks for your reply, but could you point out an alternative?

            Hosam Aly Mahmoud

            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