How to tell GUI to update controls from another thread?
-
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?
-
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?
Did it work? Please post more detailed code.
Ovidiu Cucu Microsoft MVP - Visual C++
-
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?
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. -
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?
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
-
Did it work? Please post more detailed code.
Ovidiu Cucu Microsoft MVP - Visual C++
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...