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. Message cross threads

Message cross threads

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 4 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.
  • S Offline
    S Offline
    stevenson
    wrote on last edited by
    #1

    hi, in one procedure I post message to thread with wParam, the thread accepted my message, but wParam is corrupted. Here is the code: In the procedure

    MsgParam \*pSendParam=new MsgParam;
    pSendParam->pBuf=pBuf;
    pSendParam->nLen=nLen;
    
    PostThreadMessage(tid,WM\_HYDRO\_RXD\_ARRIVAL,(WPARAM)pSendParam,0);
    

    In thread Function

    case WM_HYDRO_RXD_ARRIVAL:
    pRecParam=(MsgParam*)msg.wParam;

    before PostThreadMessage, pSendParam is correct(in this case, it's f5f5090574a05200), but in case WM_HYDRO_RXD_ARRIVAL, the value is corrupted as 44bf4a5f01000000, How to transfer data correctly? Extreme programming. Do the No.1

    S D I K 4 Replies Last reply
    0
    • S stevenson

      hi, in one procedure I post message to thread with wParam, the thread accepted my message, but wParam is corrupted. Here is the code: In the procedure

      MsgParam \*pSendParam=new MsgParam;
      pSendParam->pBuf=pBuf;
      pSendParam->nLen=nLen;
      
      PostThreadMessage(tid,WM\_HYDRO\_RXD\_ARRIVAL,(WPARAM)pSendParam,0);
      

      In thread Function

      case WM_HYDRO_RXD_ARRIVAL:
      pRecParam=(MsgParam*)msg.wParam;

      before PostThreadMessage, pSendParam is correct(in this case, it's f5f5090574a05200), but in case WM_HYDRO_RXD_ARRIVAL, the value is corrupted as 44bf4a5f01000000, How to transfer data correctly? Extreme programming. Do the No.1

      S Offline
      S Offline
      stevenson
      wrote on last edited by
      #2

      appreciation for any information on this post. Thanks Extreme programming. Do the No.1

      D 1 Reply Last reply
      0
      • S stevenson

        hi, in one procedure I post message to thread with wParam, the thread accepted my message, but wParam is corrupted. Here is the code: In the procedure

        MsgParam \*pSendParam=new MsgParam;
        pSendParam->pBuf=pBuf;
        pSendParam->nLen=nLen;
        
        PostThreadMessage(tid,WM\_HYDRO\_RXD\_ARRIVAL,(WPARAM)pSendParam,0);
        

        In thread Function

        case WM_HYDRO_RXD_ARRIVAL:
        pRecParam=(MsgParam*)msg.wParam;

        before PostThreadMessage, pSendParam is correct(in this case, it's f5f5090574a05200), but in case WM_HYDRO_RXD_ARRIVAL, the value is corrupted as 44bf4a5f01000000, How to transfer data correctly? Extreme programming. Do the No.1

        D Offline
        D Offline
        Daniel Lohmann
        wrote on last edited by
        #3

        You are developing on Win64? Wow :cool: Your problem sounds a bit strange. It should work as you described, I can't find anything conspicuous in your code. I would suppose a subtle side effect: - Are you sure the value of WM_HYDRO_RXD_ARRIVAL is unique and you are not accidently catching the wrong message? - Are you sure _W64 is always defined and really all data types are used as 64 bit types? - Did you monitor any other side effects that look like memory corruption occuring somewhere in your app? - Does the same problem appear in a small test app? (Hm the above looks a bit like the generic "are you sure the power cable is plugged in" hotline answer :-O ) Good luck! -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

        1 Reply Last reply
        0
        • S stevenson

          hi, in one procedure I post message to thread with wParam, the thread accepted my message, but wParam is corrupted. Here is the code: In the procedure

          MsgParam \*pSendParam=new MsgParam;
          pSendParam->pBuf=pBuf;
          pSendParam->nLen=nLen;
          
          PostThreadMessage(tid,WM\_HYDRO\_RXD\_ARRIVAL,(WPARAM)pSendParam,0);
          

          In thread Function

          case WM_HYDRO_RXD_ARRIVAL:
          pRecParam=(MsgParam*)msg.wParam;

          before PostThreadMessage, pSendParam is correct(in this case, it's f5f5090574a05200), but in case WM_HYDRO_RXD_ARRIVAL, the value is corrupted as 44bf4a5f01000000, How to transfer data correctly? Extreme programming. Do the No.1

          I Offline
          I Offline
          includeh10
          wrote on last edited by
          #4

          the code is absolutely incorrect. try to send an int int i=5; PostThreadMessage(tid,WM_HYDRO_RXD_ARRIVAL,(WPARAM)i,0); it will be OK. when u pass an object created by new, normally it can not be passed to another thread in the simple way. i don't know how to do it, but i did similar thing in COM, if u know COM, try same idea that how COM passes object to another COM (they are in 2 threads). good luck. includeh10

          D S 2 Replies Last reply
          0
          • I includeh10

            the code is absolutely incorrect. try to send an int int i=5; PostThreadMessage(tid,WM_HYDRO_RXD_ARRIVAL,(WPARAM)i,0); it will be OK. when u pass an object created by new, normally it can not be passed to another thread in the simple way. i don't know how to do it, but i did similar thing in COM, if u know COM, try same idea that how COM passes object to another COM (they are in 2 threads). good luck. includeh10

            D Offline
            D Offline
            Daniel Lohmann
            wrote on last edited by
            #5

            What? :wtf::omg: Sorry, includeh10, but you seem to completly misunderstood the concept of threads. You can pass pointers between threads as long as you make sure they reside in the same process. All threads inside a process share the same address space, therefore any address reference is valid and accessable by every thread. Actually this easiness of inter-thread communication is the main reason we use threads instead of processes. COM puts a lot of "magic stuff" (like apartments, free threaded marshaller, ...) around this, because it has to guarantee that it even works if both thread reside in different processes or even on different machines. -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

            I 1 Reply Last reply
            0
            • S stevenson

              appreciation for any information on this post. Thanks Extreme programming. Do the No.1

              D Offline
              D Offline
              Daniel Lohmann
              wrote on last edited by
              #6

              BTW: Your E-Mail address seems to be incorrect. I got an "Undeliverable Mail" message for steven_wng@sina.com on my previous post. Hope you check the forum as well :-) -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

              1 Reply Last reply
              0
              • D Daniel Lohmann

                What? :wtf::omg: Sorry, includeh10, but you seem to completly misunderstood the concept of threads. You can pass pointers between threads as long as you make sure they reside in the same process. All threads inside a process share the same address space, therefore any address reference is valid and accessable by every thread. Actually this easiness of inter-thread communication is the main reason we use threads instead of processes. COM puts a lot of "magic stuff" (like apartments, free threaded marshaller, ...) around this, because it has to guarantee that it even works if both thread reside in different processes or even on different machines. -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

                I Offline
                I Offline
                includeh10
                wrote on last edited by
                #7

                i am pretty sure that an object can be past from one COM to another even 2 COM are in the same process (or create by same process). as i said, i never use threads in this way, but i don't think the code would work. anyway, do further test to get right answer. good luck includeh10

                1 Reply Last reply
                0
                • S stevenson

                  hi, in one procedure I post message to thread with wParam, the thread accepted my message, but wParam is corrupted. Here is the code: In the procedure

                  MsgParam \*pSendParam=new MsgParam;
                  pSendParam->pBuf=pBuf;
                  pSendParam->nLen=nLen;
                  
                  PostThreadMessage(tid,WM\_HYDRO\_RXD\_ARRIVAL,(WPARAM)pSendParam,0);
                  

                  In thread Function

                  case WM_HYDRO_RXD_ARRIVAL:
                  pRecParam=(MsgParam*)msg.wParam;

                  before PostThreadMessage, pSendParam is correct(in this case, it's f5f5090574a05200), but in case WM_HYDRO_RXD_ARRIVAL, the value is corrupted as 44bf4a5f01000000, How to transfer data correctly? Extreme programming. Do the No.1

                  K Offline
                  K Offline
                  Kelly Herald
                  wrote on last edited by
                  #8

                  Have you tried passing pSendParam through the LPARAM parameter? PostThreadMessage(tid,WM_HYDRO_RXD_ARRIVAL,0,(LPARAM)pSendParam); and changing your thread to this: pRecParam=(MsgParam*)msg.lParam; Kelly Herald Software Developer Micronpc, LLC

                  1 Reply Last reply
                  0
                  • I includeh10

                    the code is absolutely incorrect. try to send an int int i=5; PostThreadMessage(tid,WM_HYDRO_RXD_ARRIVAL,(WPARAM)i,0); it will be OK. when u pass an object created by new, normally it can not be passed to another thread in the simple way. i don't know how to do it, but i did similar thing in COM, if u know COM, try same idea that how COM passes object to another COM (they are in 2 threads). good luck. includeh10

                    S Offline
                    S Offline
                    stevenson
                    wrote on last edited by
                    #9

                    hi, yeath, I really can pass int i through the gap,but can't pass a struct. I don't know why too. As far as COM is concerned, Inside COM and Essential COM is all what I have. I determined to check it out. Thanks Extreme programming. Do the No.1

                    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