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 problem ?????

SendMessage problem ?????

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 5 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.
  • A Offline
    A Offline
    amitmistry_petlad
    wrote on last edited by
    #1

    Once all thread started from the main thread. I have put the follwing wait message in main thread to handle the child thread.Means until completed all the child threds Main thread must be wait. for that I had put. WaitForMultipleObjects(2, hThread, FALSE, INFINITE); will it stop the messagepumping ? because when thread reach at the following line . SendMessage(hwndCmbProfile,CB_GETCURSEL,0,0); The application does stop the working .In Task manger it shown application not responding. Is WaitForMultipleObjects affect on os message pumping?????? why the application get hang??

    "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

    C S M 3 Replies Last reply
    0
    • A amitmistry_petlad

      Once all thread started from the main thread. I have put the follwing wait message in main thread to handle the child thread.Means until completed all the child threds Main thread must be wait. for that I had put. WaitForMultipleObjects(2, hThread, FALSE, INFINITE); will it stop the messagepumping ? because when thread reach at the following line . SendMessage(hwndCmbProfile,CB_GETCURSEL,0,0); The application does stop the working .In Task manger it shown application not responding. Is WaitForMultipleObjects affect on os message pumping?????? why the application get hang??

      "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

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

      amitmistry_petlad wrote:

      will it stop the messagepumping ?

      If you don't start any new thread, your application has only one thread. It means that if you do something that takes a lot of time, your application will not respond during this time, because the message loop is in the same thread. Thus, if you wait for something in the main thread, your application won't respond (unable to process messages) until the wait is terminated and until you are able to process messages once again.


      Cédric Moonen Software developer
      Charting control [v1.2]

      1 Reply Last reply
      0
      • A amitmistry_petlad

        Once all thread started from the main thread. I have put the follwing wait message in main thread to handle the child thread.Means until completed all the child threds Main thread must be wait. for that I had put. WaitForMultipleObjects(2, hThread, FALSE, INFINITE); will it stop the messagepumping ? because when thread reach at the following line . SendMessage(hwndCmbProfile,CB_GETCURSEL,0,0); The application does stop the working .In Task manger it shown application not responding. Is WaitForMultipleObjects affect on os message pumping?????? why the application get hang??

        "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Firstly this line looks funny:

        WaitForMultipleObjects(2, hThread, FALSE, INFINITE);

        If hThread is thread HANDLE, as the Hungarian prefix suggests, then this should not compile. It looks like you're just waiting on one HANDLE. I'd guess hThread is a pointer to an array of HANDLEs: if this is the case the "h" prefix is confusing and will throw programmers that are aware of Hungarian prefixes. Secondly, no message pump is running so the SendMessage in the worker thread will (and should) block. What you need is the MsgWaitForMultipleObjects function. Look in the ATL source code in the function AtlWaitWithMessageLoop for an example of its usage.

        Steve

        A 1 Reply Last reply
        0
        • A amitmistry_petlad

          Once all thread started from the main thread. I have put the follwing wait message in main thread to handle the child thread.Means until completed all the child threds Main thread must be wait. for that I had put. WaitForMultipleObjects(2, hThread, FALSE, INFINITE); will it stop the messagepumping ? because when thread reach at the following line . SendMessage(hwndCmbProfile,CB_GETCURSEL,0,0); The application does stop the working .In Task manger it shown application not responding. Is WaitForMultipleObjects affect on os message pumping?????? why the application get hang??

          "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          Messages sent to a window must be processed by the thread that created the window. When a worker thread calls SendMessage(), it blocks until the message handler returns. Since the message is going to a window (assuming it was created by the main thread, which is uaually the case), the thread that has to handle the message is blocked and can't run, so you've created a deadlock. You also have a bug in the call to WaitForMultipleObjects() - you say you're passing 2 handles, but only providing one.

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

          A 1 Reply Last reply
          0
          • M Michael Dunn

            Messages sent to a window must be processed by the thread that created the window. When a worker thread calls SendMessage(), it blocks until the message handler returns. Since the message is going to a window (assuming it was created by the main thread, which is uaually the case), the thread that has to handle the message is blocked and can't run, so you've created a deadlock. You also have a bug in the call to WaitForMultipleObjects() - you say you're passing 2 handles, but only providing one.

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

            A Offline
            A Offline
            amitmistry_petlad
            wrote on last edited by
            #5

            Thank you sir, How can I stop the deadlock situation? and child therad SendMessage to the window.

            "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

            R M 2 Replies Last reply
            0
            • S Stephen Hewitt

              Firstly this line looks funny:

              WaitForMultipleObjects(2, hThread, FALSE, INFINITE);

              If hThread is thread HANDLE, as the Hungarian prefix suggests, then this should not compile. It looks like you're just waiting on one HANDLE. I'd guess hThread is a pointer to an array of HANDLEs: if this is the case the "h" prefix is confusing and will throw programmers that are aware of Hungarian prefixes. Secondly, no message pump is running so the SendMessage in the worker thread will (and should) block. What you need is the MsgWaitForMultipleObjects function. Look in the ATL source code in the function AtlWaitWithMessageLoop for an example of its usage.

              Steve

              A Offline
              A Offline
              amitmistry_petlad
              wrote on last edited by
              #6

              Stephen Hewitt wrote:

              I'd guess hThread is a pointer to an array of HANDLEs:

              You are right sir , this is an array of HANDLES. Plese look at here sir, for(...) { **hThread[i] = (HANDLE ) _beginthreadex(NULL, 0, &Thread, l, 0, &tid)**}

              "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

              1 Reply Last reply
              0
              • A amitmistry_petlad

                Thank you sir, How can I stop the deadlock situation? and child therad SendMessage to the window.

                "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

                R Offline
                R Offline
                Roger Stoltz
                wrote on last edited by
                #7

                You should avoid the use of SendMessage() between threads; if the receiving thread is not processing messages your application will deadlock. Use PostMessage() instead. Read more here[^].


                "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                "High speed never compensates for wrong direction!" - unknown

                A 1 Reply Last reply
                0
                • R Roger Stoltz

                  You should avoid the use of SendMessage() between threads; if the receiving thread is not processing messages your application will deadlock. Use PostMessage() instead. Read more here[^].


                  "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                  "High speed never compensates for wrong direction!" - unknown

                  A Offline
                  A Offline
                  amitmistry_petlad
                  wrote on last edited by
                  #8

                  Grate ! Thanks! But my Thread application passes through some function where I every next step ,I have used the GUI messaging so now how can i handle that situation. means fetching the values from the different control like combobox ,static text, listview during the thread is runing. But thank you very much, I will try it for PostMessage.

                  "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

                  1 Reply Last reply
                  0
                  • A amitmistry_petlad

                    Thank you sir, How can I stop the deadlock situation? and child therad SendMessage to the window.

                    "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    As Stephen suggested above, AtlWaitWithMessageLoop() is one way to have a thread wait and still be able to process messages. It's the method I use.

                    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

                    A 1 Reply Last reply
                    0
                    • M Michael Dunn

                      As Stephen suggested above, AtlWaitWithMessageLoop() is one way to have a thread wait and still be able to process messages. It's the method I use.

                      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

                      A Offline
                      A Offline
                      amitmistry_petlad
                      wrote on last edited by
                      #10

                      sir, I have found the help for that and I am satisfy with this answer but could you guide me where should i write this function? I mean in my Mainthread or the successive function call from the Mainthread.(:doh:) suppose , e.g. for(....) { //thread created from here hThread[i]=(HANDLE ) _beginthreadex(NULL, 0, &Thread, l, 0, &tid); //calling from here ????:confused: } here :confused: ????? where should I call and where should I defined ? unsigned __stdcall Thread(void* pArguments ) { HRESULT hr=NULL; struct argument_list *Lparam= (argument_list *)pArguments ; Package pkg; static int threadID; threadID++; { hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); } if(hr==S_OK) { MessageBox(0,L"thread gone finished" ,L"therad",0); _endthreadex( 0 ); } //or :confused: should I write some where here??? threadID--; return 0; }

                      "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

                      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