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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. AfxBeginThread and ON_THREAD_MESSAGE does not work using PostThreadMessage

AfxBeginThread and ON_THREAD_MESSAGE does not work using PostThreadMessage

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpquestion
9 Posts 6 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.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    Could someone please explain to me why the following code does not work? I build a new thread in a view process: …. m_COpenHR_AutoCOM_Thread = (COpenHR_AutoCOM_Thread*) AfxBeginThread(ThreadFunction,this,THREAD_PRIORITY_LOWEST); VERIFY(m_COpenHR_AutoCOM_Thread); return 1; } Runs the thread function within the view class just fine. Now I want to stop the process. In property page I have this code to post thread message: CWinThread* pCurrentThread = AfxGetThread( ); if(!pCurrentThread->PostThreadMessage(WM_USER_AUTO_COM , 0, 1)) { TRACE("PostThreadMessage(WM_USER_AUTO_COM , 0, 1))"); } In the derived CWinThread class I have this: BEGIN_MESSAGE_MAP(COpenHR_AutoCOM_Thread, CWinThread) //{{AFX_MSG_MAP(COpenHR_AutoCOM_Thread) // NOTE - the ClassWizard will add and remove mapping macros here. ON_THREAD_MESSAGE( WM_USER_AUTO_COM, ControlThread ) //}}AFX_MSG_MAP END_MESSAGE_MAP() I never get to the ControlThread function. What am I missing or doing wrong? Should PostThreadMessage be directed to my new thread and how? Thanks you for your help. Vaclav

    N R R D V 6 Replies Last reply
    0
    • V Vaclav_

      Could someone please explain to me why the following code does not work? I build a new thread in a view process: …. m_COpenHR_AutoCOM_Thread = (COpenHR_AutoCOM_Thread*) AfxBeginThread(ThreadFunction,this,THREAD_PRIORITY_LOWEST); VERIFY(m_COpenHR_AutoCOM_Thread); return 1; } Runs the thread function within the view class just fine. Now I want to stop the process. In property page I have this code to post thread message: CWinThread* pCurrentThread = AfxGetThread( ); if(!pCurrentThread->PostThreadMessage(WM_USER_AUTO_COM , 0, 1)) { TRACE("PostThreadMessage(WM_USER_AUTO_COM , 0, 1))"); } In the derived CWinThread class I have this: BEGIN_MESSAGE_MAP(COpenHR_AutoCOM_Thread, CWinThread) //{{AFX_MSG_MAP(COpenHR_AutoCOM_Thread) // NOTE - the ClassWizard will add and remove mapping macros here. ON_THREAD_MESSAGE( WM_USER_AUTO_COM, ControlThread ) //}}AFX_MSG_MAP END_MESSAGE_MAP() I never get to the ControlThread function. What am I missing or doing wrong? Should PostThreadMessage be directed to my new thread and how? Thanks you for your help. Vaclav

      N Offline
      N Offline
      No Name No Name No Name
      wrote on last edited by
      #2

      AfxGetThread( ) must be called from within the thread. In your code, the pCurrentThread doesn't point to the thread you wish to send the message. Instead you must use, m_COpenHR_AutoCOM_Thread->PostThreadMessage(WM_USER_AUTO_COM , 0, 1))

      Powered by Ctrl + C Driven by Ctrl + V

      1 Reply Last reply
      0
      • V Vaclav_

        Could someone please explain to me why the following code does not work? I build a new thread in a view process: …. m_COpenHR_AutoCOM_Thread = (COpenHR_AutoCOM_Thread*) AfxBeginThread(ThreadFunction,this,THREAD_PRIORITY_LOWEST); VERIFY(m_COpenHR_AutoCOM_Thread); return 1; } Runs the thread function within the view class just fine. Now I want to stop the process. In property page I have this code to post thread message: CWinThread* pCurrentThread = AfxGetThread( ); if(!pCurrentThread->PostThreadMessage(WM_USER_AUTO_COM , 0, 1)) { TRACE("PostThreadMessage(WM_USER_AUTO_COM , 0, 1))"); } In the derived CWinThread class I have this: BEGIN_MESSAGE_MAP(COpenHR_AutoCOM_Thread, CWinThread) //{{AFX_MSG_MAP(COpenHR_AutoCOM_Thread) // NOTE - the ClassWizard will add and remove mapping macros here. ON_THREAD_MESSAGE( WM_USER_AUTO_COM, ControlThread ) //}}AFX_MSG_MAP END_MESSAGE_MAP() I never get to the ControlThread function. What am I missing or doing wrong? Should PostThreadMessage be directed to my new thread and how? Thanks you for your help. Vaclav

        R Offline
        R Offline
        ramana g
        wrote on last edited by
        #3

        I think you are creating a worker thread. As per my knowledge, you may need to create a UI thread inorder to post messages to the new thread. See the other version of AfxBeginThread function.

        S 1 Reply Last reply
        0
        • R ramana g

          I think you are creating a worker thread. As per my knowledge, you may need to create a UI thread inorder to post messages to the new thread. See the other version of AfxBeginThread function.

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

          Use User Interface thread instead worker thread.

          sthalasayanam

          1 Reply Last reply
          0
          • V Vaclav_

            Could someone please explain to me why the following code does not work? I build a new thread in a view process: …. m_COpenHR_AutoCOM_Thread = (COpenHR_AutoCOM_Thread*) AfxBeginThread(ThreadFunction,this,THREAD_PRIORITY_LOWEST); VERIFY(m_COpenHR_AutoCOM_Thread); return 1; } Runs the thread function within the view class just fine. Now I want to stop the process. In property page I have this code to post thread message: CWinThread* pCurrentThread = AfxGetThread( ); if(!pCurrentThread->PostThreadMessage(WM_USER_AUTO_COM , 0, 1)) { TRACE("PostThreadMessage(WM_USER_AUTO_COM , 0, 1))"); } In the derived CWinThread class I have this: BEGIN_MESSAGE_MAP(COpenHR_AutoCOM_Thread, CWinThread) //{{AFX_MSG_MAP(COpenHR_AutoCOM_Thread) // NOTE - the ClassWizard will add and remove mapping macros here. ON_THREAD_MESSAGE( WM_USER_AUTO_COM, ControlThread ) //}}AFX_MSG_MAP END_MESSAGE_MAP() I never get to the ControlThread function. What am I missing or doing wrong? Should PostThreadMessage be directed to my new thread and how? Thanks you for your help. Vaclav

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            You need to create a thread with a message pump so that it can receive and process your message. Right now, you're creating a background worker, which won't have a message pump. Take a look at this article to achieve what you're trying to do: www.flounder.com/uithreads.htm[^]

            “Follow your bliss.” – Joseph Campbell

            1 Reply Last reply
            0
            • V Vaclav_

              Could someone please explain to me why the following code does not work? I build a new thread in a view process: …. m_COpenHR_AutoCOM_Thread = (COpenHR_AutoCOM_Thread*) AfxBeginThread(ThreadFunction,this,THREAD_PRIORITY_LOWEST); VERIFY(m_COpenHR_AutoCOM_Thread); return 1; } Runs the thread function within the view class just fine. Now I want to stop the process. In property page I have this code to post thread message: CWinThread* pCurrentThread = AfxGetThread( ); if(!pCurrentThread->PostThreadMessage(WM_USER_AUTO_COM , 0, 1)) { TRACE("PostThreadMessage(WM_USER_AUTO_COM , 0, 1))"); } In the derived CWinThread class I have this: BEGIN_MESSAGE_MAP(COpenHR_AutoCOM_Thread, CWinThread) //{{AFX_MSG_MAP(COpenHR_AutoCOM_Thread) // NOTE - the ClassWizard will add and remove mapping macros here. ON_THREAD_MESSAGE( WM_USER_AUTO_COM, ControlThread ) //}}AFX_MSG_MAP END_MESSAGE_MAP() I never get to the ControlThread function. What am I missing or doing wrong? Should PostThreadMessage be directed to my new thread and how? Thanks you for your help. Vaclav

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

              You could use an event (e.g., CEvent) between the two threads. Signal it in the primary thread, and check it in the secondary thread using WaitForSingleObject().

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              1 Reply Last reply
              0
              • V Vaclav_

                Could someone please explain to me why the following code does not work? I build a new thread in a view process: …. m_COpenHR_AutoCOM_Thread = (COpenHR_AutoCOM_Thread*) AfxBeginThread(ThreadFunction,this,THREAD_PRIORITY_LOWEST); VERIFY(m_COpenHR_AutoCOM_Thread); return 1; } Runs the thread function within the view class just fine. Now I want to stop the process. In property page I have this code to post thread message: CWinThread* pCurrentThread = AfxGetThread( ); if(!pCurrentThread->PostThreadMessage(WM_USER_AUTO_COM , 0, 1)) { TRACE("PostThreadMessage(WM_USER_AUTO_COM , 0, 1))"); } In the derived CWinThread class I have this: BEGIN_MESSAGE_MAP(COpenHR_AutoCOM_Thread, CWinThread) //{{AFX_MSG_MAP(COpenHR_AutoCOM_Thread) // NOTE - the ClassWizard will add and remove mapping macros here. ON_THREAD_MESSAGE( WM_USER_AUTO_COM, ControlThread ) //}}AFX_MSG_MAP END_MESSAGE_MAP() I never get to the ControlThread function. What am I missing or doing wrong? Should PostThreadMessage be directed to my new thread and how? Thanks you for your help. Vaclav

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #7

                Thanks y'all! I guess I missed it somewhere in documentation that I need the message queue / pump. But I was puzzled why my CWinThread derived class was never constructed.Duh! But I am beginning to see big flaw in my design - I need to "unload" processing time from the main thread ( I hope that is in English!) so I can click the button to stop my process. But I want to stop this process at specific location , so why am I going thru all this extra code if I can just put a global flag and set it in the worker thread? What would be the difference between this (flag) and setting an event in worker thread and waiting for it in the main thread as suggested? (BTW very good suggestion,thanks) I prefer KISS methods! I was just trying to learn how to communicate between threads for future reference when the task won't be so simple. Anyway, can someone elaborate on this: if I create worker thread variable in one thread (as I did ) how can I use that variable ( to control the thread as I wanted originaly) from another class ( object )? Do I need a global variable? How would that appoach differ from my use of simple flag? But I think I am getting too far into left field of what - if scenario. Thanks again for all you suggestions, I really appreciate it. Vaclav PS Spring is finally here in Texas! Success!

                V 1 Reply Last reply
                0
                • V Vaclav_

                  Thanks y'all! I guess I missed it somewhere in documentation that I need the message queue / pump. But I was puzzled why my CWinThread derived class was never constructed.Duh! But I am beginning to see big flaw in my design - I need to "unload" processing time from the main thread ( I hope that is in English!) so I can click the button to stop my process. But I want to stop this process at specific location , so why am I going thru all this extra code if I can just put a global flag and set it in the worker thread? What would be the difference between this (flag) and setting an event in worker thread and waiting for it in the main thread as suggested? (BTW very good suggestion,thanks) I prefer KISS methods! I was just trying to learn how to communicate between threads for future reference when the task won't be so simple. Anyway, can someone elaborate on this: if I create worker thread variable in one thread (as I did ) how can I use that variable ( to control the thread as I wanted originaly) from another class ( object )? Do I need a global variable? How would that appoach differ from my use of simple flag? But I think I am getting too far into left field of what - if scenario. Thanks again for all you suggestions, I really appreciate it. Vaclav PS Spring is finally here in Texas! Success!

                  V Offline
                  V Offline
                  Vaclav_
                  wrote on last edited by
                  #8

                  Addendum I decided to try the UI thread to stop the process. Seems like a challenge and I like to torture myself with goofy programming techniques. Now I cannot figure out how to execute the process I was after. This process ( used to be worker thread "function") and its 300+ lines of code is in the view main thread! It is OK to make the view variable in the new UI thread? On first thought it seems like "circular reference". Any other way to do this “ function transfer” from the view? Thanks for all you help. Vaclav

                  1 Reply Last reply
                  0
                  • V Vaclav_

                    Could someone please explain to me why the following code does not work? I build a new thread in a view process: …. m_COpenHR_AutoCOM_Thread = (COpenHR_AutoCOM_Thread*) AfxBeginThread(ThreadFunction,this,THREAD_PRIORITY_LOWEST); VERIFY(m_COpenHR_AutoCOM_Thread); return 1; } Runs the thread function within the view class just fine. Now I want to stop the process. In property page I have this code to post thread message: CWinThread* pCurrentThread = AfxGetThread( ); if(!pCurrentThread->PostThreadMessage(WM_USER_AUTO_COM , 0, 1)) { TRACE("PostThreadMessage(WM_USER_AUTO_COM , 0, 1))"); } In the derived CWinThread class I have this: BEGIN_MESSAGE_MAP(COpenHR_AutoCOM_Thread, CWinThread) //{{AFX_MSG_MAP(COpenHR_AutoCOM_Thread) // NOTE - the ClassWizard will add and remove mapping macros here. ON_THREAD_MESSAGE( WM_USER_AUTO_COM, ControlThread ) //}}AFX_MSG_MAP END_MESSAGE_MAP() I never get to the ControlThread function. What am I missing or doing wrong? Should PostThreadMessage be directed to my new thread and how? Thanks you for your help. Vaclav

                    V Offline
                    V Offline
                    Vaclav_
                    wrote on last edited by
                    #9

                    "Progress" report: I have managed to execute an equivalent of "worker thread function" located in the UI thread class. Two problems: 1. It needs access to document and it asserts there. I am working on why. 2. The big one: If I temporary bypass the code( won't work witout the document) and just do an infinite loop ( even with some Sleep in it) I cannot click the UI button! That means only one thing - the OS is too busy letting the main thread messages thru! That is not what I was after in the first place! If I continue with multithreaded approach (just for kiks) I need to write some more code! Joy!?

                    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