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. MFC Worker Thread Question

MFC Worker Thread Question

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++tutorial
16 Posts 7 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.
  • P Prakash Nadar

    Update data to what ? is it of the dialog? is so where is the pointer to the dialog window ? I hope i answered ur question with questions. :-)


    "When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)

    D Offline
    D Offline
    Dev578
    wrote on last edited by
    #3

    I wan't the worker thread to update the data of the dialog.

    J 1 Reply Last reply
    0
    • D Dev578

      I wan't the worker thread to update the data of the dialog.

      J Offline
      J Offline
      J B 0
      wrote on last edited by
      #4

      UINT WorkerThread(LPVOID pParam)
      {
      CMyDialog *mydlg_ptr = (CMyDialog *)pParam;

      while (1==1)
      {
      mydlg_ptr->UpdateData();
      //do something
      mydlg_ptr->UpdateData(false);
      }
      return 0;
      }

      void main()
      {
      AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL);
      }

      would that make any difference?

      D 1 Reply Last reply
      0
      • D Dev578

        Here is the problem I am having: UINT WorkerThread(LPVOID pParam) { while (1==1) { UpdateData(); //do something UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL); } The UpdateData() function does not work in the Worker Thread. I have researched this quite a bit, and apparantly others were having the same problem. However, no one was able to come up with a straight answer. Anyone know how to use UpdateData() in a worker thread? Any help is appreciated. -Dev578

        P Offline
        P Offline
        Prakash Nadar
        wrote on last edited by
        #5

        Can you elaborate about the updatedata function ? I mean how u are using it, it obviously not a MFC code. You are not clear with the question.


        "When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)

        D 1 Reply Last reply
        0
        • J J B 0

          UINT WorkerThread(LPVOID pParam)
          {
          CMyDialog *mydlg_ptr = (CMyDialog *)pParam;

          while (1==1)
          {
          mydlg_ptr->UpdateData();
          //do something
          mydlg_ptr->UpdateData(false);
          }
          return 0;
          }

          void main()
          {
          AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL);
          }

          would that make any difference?

          D Offline
          D Offline
          Dev578
          wrote on last edited by
          #6

          I have tried that already, causes an assertion failure:( -Dev578

          J B 2 Replies Last reply
          0
          • P Prakash Nadar

            Can you elaborate about the updatedata function ? I mean how u are using it, it obviously not a MFC code. You are not clear with the question.


            "When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)

            D Offline
            D Offline
            Dev578
            wrote on last edited by
            #7

            What I am doing is writing my own scripting language in MFC. It is done in a text box, the program recognizes functions that the user types in the text box, and executes them accordingly. The user might type something that loops repeadedly, in which case it would have to be done in another thread, as to not interfear with the main dialog. I have a value variable on the text box that I am using for functions like find(), mid(), etc... It only works if it has UpdateData() before it and UpdateData(false) after it. This works completely fine when done in the main thread, but I am having problems doing it in a worker thread. I have tried doing what J.B. suggested, but it caused an assertion failure. Any way to do this? -Dev578

            P 1 Reply Last reply
            0
            • D Dev578

              I have tried that already, causes an assertion failure:( -Dev578

              J Offline
              J Offline
              J B 0
              wrote on last edited by
              #8

              whoops, sorry,

              void main()
              {
              AfxBeginThread(WorkerThread, this, THREAD_PRIORITY_NORMAL);
              }
              

              get rid of (LPVOID)

              1 Reply Last reply
              0
              • D Dev578

                Here is the problem I am having: UINT WorkerThread(LPVOID pParam) { while (1==1) { UpdateData(); //do something UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL); } The UpdateData() function does not work in the Worker Thread. I have researched this quite a bit, and apparantly others were having the same problem. However, no one was able to come up with a straight answer. Anyone know how to use UpdateData() in a worker thread? Any help is appreciated. -Dev578

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

                You can't (or shouldn't) access MFC objects between threads. The dialog is created and managed by the main thread, accessing the CDialog object from another thread will often fail, as you've seen. This is just how MFC works. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas."   -- Buffy

                B 1 Reply Last reply
                0
                • D Dev578

                  What I am doing is writing my own scripting language in MFC. It is done in a text box, the program recognizes functions that the user types in the text box, and executes them accordingly. The user might type something that loops repeadedly, in which case it would have to be done in another thread, as to not interfear with the main dialog. I have a value variable on the text box that I am using for functions like find(), mid(), etc... It only works if it has UpdateData() before it and UpdateData(false) after it. This works completely fine when done in the main thread, but I am having problems doing it in a worker thread. I have tried doing what J.B. suggested, but it caused an assertion failure. Any way to do this? -Dev578

                  P Offline
                  P Offline
                  Prakash Nadar
                  wrote on last edited by
                  #10

                  Did ya check what caused the assertion? Like is it because of invalid m_hWnd, You can do so by clicking on retry at the assertion box. It will take you to the line that caused the assertion. Assertion is mostly called due to null pointers in MFC. Thread will start doing its job the moment you created it in this case... So it is calling update even before the window is created. Instead in the thread do the following check

                  if(!::IsWindow(pDlg->m_hWnd))
                  {
                  return 0;
                  }


                  "When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)

                  1 Reply Last reply
                  0
                  • D Dev578

                    I have tried that already, causes an assertion failure:( -Dev578

                    B Offline
                    B Offline
                    Balkrishna Talele
                    wrote on last edited by
                    #11

                    UINT WorkerThread(LPVOID pThis) { CMYClass *pThisObject = static_cast<CMyClass *>(pThis); while (1==1) { pThisObject->UpdateData();//do something ThisObject->UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, reinterpret_cast<CMYclass*>(this), THREAD_PRIORITY_NORMAL); } AND DEFINITLY THIS SHOULD WORK!!!! I HAD TRIED A LOT OF TIMES...NO ASSTER OR NO ERROR WERE OCCURED!!! cheers Balkrishna Talele

                    N 1 Reply Last reply
                    0
                    • M Michael Dunn

                      You can't (or shouldn't) access MFC objects between threads. The dialog is created and managed by the main thread, accessing the CDialog object from another thread will often fail, as you've seen. This is just how MFC works. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas."   -- Buffy

                      B Offline
                      B Offline
                      Balkrishna Talele
                      wrote on last edited by
                      #12

                      Hello Michel, I have had seen many times, Coders doing this UINT WorkerThread(LPVOID pThis) { CMYClass *pThisObject = static_cast<CMyClass *>(pThis); while (1==1) { pThisObject->UpdateData();//do something ThisObject->UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, reinterpret_cast<CMYclass*>(this), THREAD_PRIORITY_NORMAL); } So is this still bad idea cheers Balkrishna Talele

                      1 Reply Last reply
                      0
                      • D Dev578

                        Here is the problem I am having: UINT WorkerThread(LPVOID pParam) { while (1==1) { UpdateData(); //do something UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL); } The UpdateData() function does not work in the Worker Thread. I have researched this quite a bit, and apparantly others were having the same problem. However, no one was able to come up with a straight answer. Anyone know how to use UpdateData() in a worker thread? Any help is appreciated. -Dev578

                        N Offline
                        N Offline
                        Neville Franks
                        wrote on last edited by
                        #13

                        In MFC you can't access any CWnd derived objects created in one thread from another, well at least not easilly. You will just get lots of ASSERT's in a Debug build and it simply won't work. One way to do what you want is to use PostMessage() to post a WM_USER_xx message you define. Post this from the worker thread to the Dlg. In the dialog code add a MsgHandler for the WM_USER_xx msg and get it to call UpdateData(). This will work a treat. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

                        D 1 Reply Last reply
                        0
                        • B Balkrishna Talele

                          UINT WorkerThread(LPVOID pThis) { CMYClass *pThisObject = static_cast<CMyClass *>(pThis); while (1==1) { pThisObject->UpdateData();//do something ThisObject->UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, reinterpret_cast<CMYclass*>(this), THREAD_PRIORITY_NORMAL); } AND DEFINITLY THIS SHOULD WORK!!!! I HAD TRIED A LOT OF TIMES...NO ASSTER OR NO ERROR WERE OCCURED!!! cheers Balkrishna Talele

                          N Offline
                          N Offline
                          Neville Franks
                          wrote on last edited by
                          #14

                          If pThisObject is derived from CWnd and the object was created in another thread this won't work. See: http://www.codeproject.com/script/comments/forums.asp?msg=745380&forumid=1647#xx745380xx[^] Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

                          1 Reply Last reply
                          0
                          • N Neville Franks

                            In MFC you can't access any CWnd derived objects created in one thread from another, well at least not easilly. You will just get lots of ASSERT's in a Debug build and it simply won't work. One way to do what you want is to use PostMessage() to post a WM_USER_xx message you define. Post this from the worker thread to the Dlg. In the dialog code add a MsgHandler for the WM_USER_xx msg and get it to call UpdateData(). This will work a treat. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

                            D Offline
                            D Offline
                            Dev578
                            wrote on last edited by
                            #15

                            I appreciate all of your help:) I think I understand it now, just having a slight problem. How do you make an eventhandler that fires when an integer changes? Again, thanks for all the help. -Dev578

                            1 Reply Last reply
                            0
                            • D Dev578

                              Here is the problem I am having: UINT WorkerThread(LPVOID pParam) { while (1==1) { UpdateData(); //do something UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL); } The UpdateData() function does not work in the Worker Thread. I have researched this quite a bit, and apparantly others were having the same problem. However, no one was able to come up with a straight answer. Anyone know how to use UpdateData() in a worker thread? Any help is appreciated. -Dev578

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

                              First off, remove the calls to UpdateData(), and use member control variables instead. Second, what you've created is a "worker" thread, or a thread with no message pump. A thread with no message pump should in no way be interacting with the UI. That is what a "UI" thread, or a thread with a message pump, is for. In the UI thread, simply post a message to the primary thread, the one that owns the UI, with what's to be updated.


                              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                              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