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 Hangs

SendMessage Hangs

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondatabasedesignjson
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
    SelvaKr
    wrote on last edited by
    #1

    Hi, I have 2 threads in my app. one is main application thread which has UI. second is the worker thread. second thread will get some status from a database and send the information to the main thread using SendMessageAPI. One of the parameters in the SendMessage API is a global variable across the threads. After the recieving the message from second thread, first thread will display the error to the user and return the control so that the second thread can monitor the status continuously. But at some point, my app hangs. I suspect that the hang happens in this loop. As the return depends on the response from the main thread (thru user input via messagebox), i could not use SendMessageTimeout API in this case. 1. Is it because of the global variable usage in SendMessage function? Can you please let me know how can i come out of this problem. Any help is appreciated.

    Selva

    C 1 Reply Last reply
    0
    • S SelvaKr

      Hi, I have 2 threads in my app. one is main application thread which has UI. second is the worker thread. second thread will get some status from a database and send the information to the main thread using SendMessageAPI. One of the parameters in the SendMessage API is a global variable across the threads. After the recieving the message from second thread, first thread will display the error to the user and return the control so that the second thread can monitor the status continuously. But at some point, my app hangs. I suspect that the hang happens in this loop. As the return depends on the response from the main thread (thru user input via messagebox), i could not use SendMessageTimeout API in this case. 1. Is it because of the global variable usage in SendMessage function? Can you please let me know how can i come out of this problem. Any help is appreciated.

      Selva

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Please post some code: from your worker thread when you send the message and from the main thread where you handle this message. By 'hangs' you mean that your UI freeze completely and you can't do anything anymore with your app ?

      Cédric Moonen Software developer
      Charting control [v1.4] OpenGL game tutorial in C++

      S 1 Reply Last reply
      0
      • C Cedric Moonen

        Please post some code: from your worker thread when you send the message and from the main thread where you handle this message. By 'hangs' you mean that your UI freeze completely and you can't do anything anymore with your app ?

        Cédric Moonen Software developer
        Charting control [v1.4] OpenGL game tutorial in C++

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

        Hi, Thanks for the reply. Code will look like below. int g_nInt; Main thread LRESULT CMyDlg::OnProcessMsg(WPARAM wparam, LPARAM lparam) { Display Message box ....... Get result from user. return 0; } Worker thread { get status from DB. GetMainWnd()->SendMessage(PROC_MSG, wp, g_nInt); ..... GetMainWnd()->SendMessage(PROC_MSG2, wp, lp); ......... } Yes. My main app window looks like freezed (continuous even after an hour). Thanks in advance.

        Selva

        C K 2 Replies Last reply
        0
        • S SelvaKr

          Hi, Thanks for the reply. Code will look like below. int g_nInt; Main thread LRESULT CMyDlg::OnProcessMsg(WPARAM wparam, LPARAM lparam) { Display Message box ....... Get result from user. return 0; } Worker thread { get status from DB. GetMainWnd()->SendMessage(PROC_MSG, wp, g_nInt); ..... GetMainWnd()->SendMessage(PROC_MSG2, wp, lp); ......... } Yes. My main app window looks like freezed (continuous even after an hour). Thanks in advance.

          Selva

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          SendMessage is a blocking function: it will only return once the message has been processed by the other window. In your case, it will return only after the user has closed the MessageBox. You didn't provide enough code for me to verify this but you probably have a deadlock: your worker thread is waiting for the UI thread (because of the SendMessage function) which is in turn waiting for the worker thread. If your design allows it, try with PostMessage instead of SendMessage: PostMessage puts the message in the message queue of the window and returns immediately.

          Cédric Moonen Software developer
          Charting control [v1.4] OpenGL game tutorial in C++

          S 1 Reply Last reply
          0
          • C Cedric Moonen

            SendMessage is a blocking function: it will only return once the message has been processed by the other window. In your case, it will return only after the user has closed the MessageBox. You didn't provide enough code for me to verify this but you probably have a deadlock: your worker thread is waiting for the UI thread (because of the SendMessage function) which is in turn waiting for the worker thread. If your design allows it, try with PostMessage instead of SendMessage: PostMessage puts the message in the message queue of the window and returns immediately.

            Cédric Moonen Software developer
            Charting control [v1.4] OpenGL game tutorial in C++

            S Offline
            S Offline
            SelvaKr
            wrote on last edited by
            #5

            Hi, Thanks for the reply. Main thread won't be waiting for the worker thread.. only if the second request from the worker thread comes, it will be processing. But i too suspect that its because of the deadlock. But my doubt is, whether passing the global variable (which will be accessed by both the threads) causes this hang. any way storing the value in global variable will happen with proper locks from both the threads. But whether passing this as the parameter to the SendMessage causes any problem? Thanks in advance.

            Selva

            C 1 Reply Last reply
            0
            • S SelvaKr

              Hi, Thanks for the reply. Main thread won't be waiting for the worker thread.. only if the second request from the worker thread comes, it will be processing. But i too suspect that its because of the deadlock. But my doubt is, whether passing the global variable (which will be accessed by both the threads) causes this hang. any way storing the value in global variable will happen with proper locks from both the threads. But whether passing this as the parameter to the SendMessage causes any problem? Thanks in advance.

              Selva

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Sorry but your question is not really clear. Please post some more relevant code instead of explaining what your code does. Anyway, why do you pass the value in the message ? It is globally available so, there's no need to pass it in the message. But accessing this variable from two threads won't make your UI hang (unless you are using critical section to access it, then the problem might come from that).

              SelvaKr wrote:

              any way storing the value in global variable will happen with proper locks from both the threads.

              I'm not sure what that means but if you are using critical section, then this is probably the problem. Did you try with PostMessage as I told you ? What happened ?

              Cédric Moonen Software developer
              Charting control [v1.4] OpenGL game tutorial in C++

              S 1 Reply Last reply
              0
              • C Cedric Moonen

                Sorry but your question is not really clear. Please post some more relevant code instead of explaining what your code does. Anyway, why do you pass the value in the message ? It is globally available so, there's no need to pass it in the message. But accessing this variable from two threads won't make your UI hang (unless you are using critical section to access it, then the problem might come from that).

                SelvaKr wrote:

                any way storing the value in global variable will happen with proper locks from both the threads.

                I'm not sure what that means but if you are using critical section, then this is probably the problem. Did you try with PostMessage as I told you ? What happened ?

                Cédric Moonen Software developer
                Charting control [v1.4] OpenGL game tutorial in C++

                S Offline
                S Offline
                SelvaKr
                wrote on last edited by
                #7

                Hi, Thanks for the reply. I will try postmessage instead of SendMessage and reply in case of any problems. Thanks a lot.

                Selva

                1 Reply Last reply
                0
                • S SelvaKr

                  Hi, Thanks for the reply. Code will look like below. int g_nInt; Main thread LRESULT CMyDlg::OnProcessMsg(WPARAM wparam, LPARAM lparam) { Display Message box ....... Get result from user. return 0; } Worker thread { get status from DB. GetMainWnd()->SendMessage(PROC_MSG, wp, g_nInt); ..... GetMainWnd()->SendMessage(PROC_MSG2, wp, lp); ......... } Yes. My main app window looks like freezed (continuous even after an hour). Thanks in advance.

                  Selva

                  K Offline
                  K Offline
                  krmed
                  wrote on last edited by
                  #8

                  Perhaps your problem is using the MainWnd object across threads (you didn't say if this is MFC or not, but I suspect it is). It would be safer to either pass the hWnd of your main window in the thread parameters when you start the thread, or to use GetSafeHwnd() when you send your message. For example:

                  Worker thread
                  {
                  get status from DB.
                  ::SendMessage(GetMainWnd()->GetSafeHwnd(),PROC_MSG, wp, g_nInt)
                  .....
                  ::SendMessage(GetMainWnd()->GetSafeHwnd(),PROC_MSG, wp, g_nInt)
                  .........
                  }

                  Also, you say the code will "look like" below. Post your actual code, including how you start the thread. Hope that helps.

                  Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                  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