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. How to tell GUI to update controls from another thread?

How to tell GUI to update controls from another thread?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionannouncement
5 Posts 4 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.
  • K Offline
    K Offline
    K_S
    wrote on last edited by
    #1

    I am developing an SDI application that communicates with an instrument. The application have 2 threads: GUI and a communication thread. Until now I have used the following method to deliver data from the comm-thread to the GUI: ViewWnd = ((CFrameWnd *)AfxGetApp()->m_pMainWnd)->GetActiveView(); ViewWnd->PostMessage(WM_MY_MESSAGE, 0, (LPARAM)PostString); Is this a good solution? If not? What do You suggest?

    O V C 3 Replies Last reply
    0
    • K K_S

      I am developing an SDI application that communicates with an instrument. The application have 2 threads: GUI and a communication thread. Until now I have used the following method to deliver data from the comm-thread to the GUI: ViewWnd = ((CFrameWnd *)AfxGetApp()->m_pMainWnd)->GetActiveView(); ViewWnd->PostMessage(WM_MY_MESSAGE, 0, (LPARAM)PostString); Is this a good solution? If not? What do You suggest?

      O Offline
      O Offline
      ovidiucucu
      wrote on last edited by
      #2

      Did it work? Please post more detailed code.

      Ovidiu Cucu Microsoft MVP - Visual C++

      K 1 Reply Last reply
      0
      • K K_S

        I am developing an SDI application that communicates with an instrument. The application have 2 threads: GUI and a communication thread. Until now I have used the following method to deliver data from the comm-thread to the GUI: ViewWnd = ((CFrameWnd *)AfxGetApp()->m_pMainWnd)->GetActiveView(); ViewWnd->PostMessage(WM_MY_MESSAGE, 0, (LPARAM)PostString); Is this a good solution? If not? What do You suggest?

        V Offline
        V Offline
        Viorel
        wrote on last edited by
        #3

        In my opinion an alternative solution to PostMessage is adding of a member function to your view, and then calling it directly:

        CMyView * ViewWnd = (CMyView*)(((CFrameWnd *)AfxGetApp()->m_pMainWnd)->GetActiveView());
        ViewWnd->MyUpdateFunction(PostString);
        

        I think this works faster comparing with PostMessage. I hope this helps.

        1 Reply Last reply
        0
        • K K_S

          I am developing an SDI application that communicates with an instrument. The application have 2 threads: GUI and a communication thread. Until now I have used the following method to deliver data from the comm-thread to the GUI: ViewWnd = ((CFrameWnd *)AfxGetApp()->m_pMainWnd)->GetActiveView(); ViewWnd->PostMessage(WM_MY_MESSAGE, 0, (LPARAM)PostString); Is this a good solution? If not? What do You suggest?

          C Offline
          C Offline
          cmk
          wrote on last edited by
          #4

          That's fine, you just have to make sure PostString persists long enough (and unaltered) to be handled by the GUI thread. One way to do this is alloc PostString in comm thread, and free PostString in GUI thread. If you are sending a _lot_ of strings then the repeated alloc/free overhead _may_ become an issue.

          ...cmk Save the whales - collect the whole set

          1 Reply Last reply
          0
          • O ovidiucucu

            Did it work? Please post more detailed code.

            Ovidiu Cucu Microsoft MVP - Visual C++

            K Offline
            K Offline
            K_S
            wrote on last edited by
            #5

            It does work. I just wondered if this is good solution. Here's more detailed code: commthread: ViewWnd = ((CFrameWnd *)AfxGetApp()->m_pMainWnd)->GetActiveView(); CString *PostString = new CString; *PostString = "This is a test"; ViewWnd->PostMessage(WM_DATA_UPDATED, 0, (LPARAM)PostString); GUI: ON_MESSAGE(WM_DATA_UPDATED, OnDataUpdated) long CZFGMCommunicatorView::OnDataUpdated(UINT wParam, LONG lParam) { CString* InString= (CString*)lParam; GetStatusBar()->SetPaneText(0, str1 + *InString, 1); delete InString; } What I wanted to know: Is the view is the best class to handle this or is the frame a better choise. Or if it is better to just write a member function in the gui and access this via a pointer to gui...

            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