0

The following code assumes MFC document view architecture. doc_data is a CString data member of the document. The issue is that calling UpdateAllViews causes doc_data to be displayed, but the string that is displayed has not been assigned until after the call to UpdateAllViews. This behaviour can be prevented by uncommenting the message box line so as to force a moment's wait.

// CMainFrame message handlers
void CMainFrame::OnViewTest()
{
    CView* vwd = (GetActiveView()); 
    CMFCApplicationxxDoc* pDoc{};
    if (vwd)
        pDoc = dynamic_cast<CMFCApplicationxxDoc*>(vwd->GetDocument());
    if (pDoc) {
        pDoc->doc_data = "New data"; 
        pDoc->UpdateAllViews(NULL); 
        //AfxMessageBox(_T("Wait a moment"));
        pDoc->doc_data = "Even newer data";
    }
}

This has been previously explained: "The OnUpdate function typically invalidates the view, which will cause OnDraw later. UpdateAllViews returns after the invalidating and before the painting" in Is MFC UpdateAllViews blocking or non blocking?

This could cause a problem. For example, if data is shown in a graph, some values might be updated, and some not. So my question is: What is a good way to ensure (without user input) that all current data is displayed correctly before the program goes on to modify it?

Community
  • 1
  • 1
freeze
  • 75
  • 5
  • 1
    You are running on a single thread. Everything is executed sequentially. You are asking about a problem that doesn't exist. – IInspectable Dec 31 '16 at 21:05
  • @IInspectable The problem does exist, in a way. Apparently, `UpdateAllViews` doesn't actually update views right there - it merely invalidates them, placing `WM_PAINT` into the message queue. The actual drawing happens after `UpdateAllViews` returns, once the call stack unwinds to the message pump. By that time, `doc_data` is set to `"Even newer data"`, but the OP apparently wants `"New data"` to be rendered. – Igor Tandetnik Jan 01 '17 at 00:59
  • 1
    Why do you want your views to render something other than the current, most recent data in the document? I submit the solution is to not update `doc_data` with new data until you are actually ready to render said data. Your problem stems from trying to go against the grain of doc/view model. – Igor Tandetnik Jan 01 '17 at 01:09
  • 1
    Here is my concern. Suppose the program is running some time consuming function with a lot of data, such as a weather prediction model. Every so often, it calls UpdateAllViews in order to display an updated map. It continues to calculate, and by the time the map is actually displayed some of the calculated variables have changed while others have not. so for example, wind speeds might be from the previous calculation cycle, while temperatures are based on more recent calculations. – freeze Jan 01 '17 at 01:15
  • 2
    Have a variable for the data ready to be rendered, and a separate variable for the data under construction. Once the new frame is ready, swap the two and issue `UpdateAllViews` – Igor Tandetnik Jan 01 '17 at 02:19
  • Yes. In other words, buffer it. I learned something today, so its worth it. – freeze Jan 01 '17 at 02:45
  • In fact, I noticed this in a different context. A nasty bug that was difficult to track down. It turned out that a object that was used by the view's OnDraw function had gone out of scope before it was needed. – freeze Jan 01 '17 at 02:50
  • This is staring to sound like you have multiple threads, and don't know, how to synchronize them. If that is the case, you have asked the wrong question. – IInspectable Jan 01 '17 at 09:03

0 Answers0