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. Multi-threading

Multi-threading

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

    I open a window A and from this, another window B for which the window A is the parent window. In window B there is a button with a label "Start" that starts the execution of the thread "Train". This thread must get data from the parent window and perform some calculations, and finally, update some controls in window A. I use AfxBeginThread (Train, this) from B window and inside the thread function I get a pointer to the parent window by means of GetParent function. However this does not work. Can anyone help me?:confused:

    N J M A S 5 Replies Last reply
    0
    • A amarg

      I open a window A and from this, another window B for which the window A is the parent window. In window B there is a button with a label "Start" that starts the execution of the thread "Train". This thread must get data from the parent window and perform some calculations, and finally, update some controls in window A. I use AfxBeginThread (Train, this) from B window and inside the thread function I get a pointer to the parent window by means of GetParent function. However this does not work. Can anyone help me?:confused:

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      Can you show some code snippets? Nish [Signature temporarily down]

      1 Reply Last reply
      0
      • A amarg

        I open a window A and from this, another window B for which the window A is the parent window. In window B there is a button with a label "Start" that starts the execution of the thread "Train". This thread must get data from the parent window and perform some calculations, and finally, update some controls in window A. I use AfxBeginThread (Train, this) from B window and inside the thread function I get a pointer to the parent window by means of GetParent function. However this does not work. Can anyone help me?:confused:

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        In the lack of more info, I'd venture to say the problem is that you are trying to call some member function (or use some member variable) of the CWnd * returned by GetParent (possibly cast to CAWnd * or whatever your class is named). Is this right? If so, then the problem is that that CWnd * is not actually the original object; this is due to inherent limitations of MFC with respect to multithreading. To solve the problem, pass the thread a pointer to A instead of a pointer to B (and have A store somehow a pointer to B so that you have access to both windows). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        1 Reply Last reply
        0
        • A amarg

          I open a window A and from this, another window B for which the window A is the parent window. In window B there is a button with a label "Start" that starts the execution of the thread "Train". This thread must get data from the parent window and perform some calculations, and finally, update some controls in window A. I use AfxBeginThread (Train, this) from B window and inside the thread function I get a pointer to the parent window by means of GetParent function. However this does not work. Can anyone help me?:confused:

          M Offline
          M Offline
          Morozov Alexey
          wrote on last edited by
          #4

          All threads in MFC has own CWnd-HWND map and it don't initialize while you create new thread due to perfomance reason. A common solution is to put HWND in thread-parameters and in child thread should construct CWnd by means CWnd::FromHandle function. Good luck

          1 Reply Last reply
          0
          • A amarg

            I open a window A and from this, another window B for which the window A is the parent window. In window B there is a button with a label "Start" that starts the execution of the thread "Train". This thread must get data from the parent window and perform some calculations, and finally, update some controls in window A. I use AfxBeginThread (Train, this) from B window and inside the thread function I get a pointer to the parent window by means of GetParent function. However this does not work. Can anyone help me?:confused:

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

            I finally solved the problem by using the following approach: In the cpp file of the child window B I declared a global object of type "window A". Then in the InitDialog member function of window B I called the GetParent function and I copied its return to the global object of type "window A". In the next step I used this global object from my thread function and it worked !! I also noticed that the GetParent function it works if it is called from the thread function but only the MFC based procedures of the parent window are accessible - in this way I managed to disable a button of the parent window A from the thread function of the child window B. However if the parent window A has additional member functions (non MFC based)they are not accessible in this way - but they are, if we use the method of the global object described above. Thanks all of you for your response Athanasios Margaris University of Macedonia Dept. of Applied Informatics Thessaloniki Greece Web Site : http://www.neural.uom.gr (most in Greek) :)

            1 Reply Last reply
            0
            • A amarg

              I open a window A and from this, another window B for which the window A is the parent window. In window B there is a button with a label "Start" that starts the execution of the thread "Train". This thread must get data from the parent window and perform some calculations, and finally, update some controls in window A. I use AfxBeginThread (Train, this) from B window and inside the thread function I get a pointer to the parent window by means of GetParent function. However this does not work. Can anyone help me?:confused:

              S Offline
              S Offline
              Stephen C Steel
              wrote on last edited by
              #6

              A CWnd is really two distinct entities: a C++ object which you access using a CWnd* (typically this), and the Window object created by the system, which you access using a window hande (HWND). In order for MFC to make these two separate entities work like one, it must frequently find the HWND associated with a particular CWnd object, and or find the CWnd object associated with a particular HWND.

              The first operation, mapping from CWnd to the associated HWND is easy (just look up the m_hWnd member variable). This works fine in any thread.

              The second mapping, from HWND to CWnd* uses what is know as a handle map (a table which tracks which HWNDs were created by CWnd objets in that thread). This handle map is thread specific. Furthermore, since you can create HWNDs without even creating a CWnd object (e.g. dialog controls created by the dialog template), MFC sometimes creates generic temporary CWnd objects which you can use for the duration of a message handler to access these HWNDs via a CWnd object.

              The GetParent() member function just calls the Windows API funtion ::GetParent (m_hWnd), which returns the HWND of the parent. MFC then uses the (thread specific) handle maps to get a CWnd object associated with the parent HWND. If the parent window was not created via a CWnd object in the same thread, this will a temporary generic CWnd object. So in your case, when the second thread calls GetParent() it gets a pointer to a temporary generic CWnd and NOT the CMyCWndDerivedClass object that created the parent HWND in the first thread. If you cast the CWnd pointer to a pointer your derived class, you'll acess whatever memory just happens to sit after the temporary CWnd object.

              You can confirm this with

              CWmd *pWnd = GetParent ();
              ASSERT_KINDOF (CMyCWndDerivedClass, pWnd);

              This will work in your main thread, but fail in your derived thread.

              When working with multiple threads, it is much simpler to have only the original main thread responsible for the GUI. Worker threads can update data, and then use ::PostMessage() with an HWND to send a message the main thread (that created the HWND) that it should update its appearance. Since all windows messages are handled by the single GUI thread that creates all HWNDs, the handle maps work just as expected in a single threaded application. It you let multiple threads deal with HWNDs and CWnd objects directly, then you need to understand temporary and permanent handle maps AND when and how they are used by MFC. Believe me, the first approac

              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