4

I have a MFC code based on Document View framework. I use UpdateAllViews(nullptr,0,nullptr) from Document class to invoke the View's OnDraw member function.

void MyDocumentClass::MyFunction()
{
    //.. Document code to create and process data
    UpdateAllViews(nullptr,0,nullptr) // Invokes OnDraw
    // When does program control reach this line? 
}

My question is, please tell me if the UpdateAllViews function is blocking or non blocking, when does program control reach the line next to UpdateAllViews()? Does it reach there after all the code in OnDraw() has finished executing, or does it reach there sooner?

The Vivandiere
  • 3,059
  • 3
  • 28
  • 50

2 Answers2

7

UpdateAllViews is a nonblocking function that simply calls OnUpdate of each view. The OnUpdate function typically invalidates the view, which will cause OnDraw later. UpdateAllViews returns after the invalidating and before the painting.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
2

UpdateAllViews is a blocking function that simply loops each view and calls their OnUpdate function. It is not a "queue for later" and return immediately call like PostMessage.

Like SendMessage, UpdateAllViews does not return until all of the code in each view's OnUpdate function has been executed. That is why doing something heavy like like directly calling OnDraw or blocking I/O in UpdateAllViews/OnUpdate is typically a bad idea. It is a better practice to invalidate some or all of the views based on the hint parameters and let the framework call OnDraw on the next WM_PAINT.

jla
  • 6,904
  • 2
  • 36
  • 34
  • There are now two totally contradictory answers to this question, but they're both right. It all depends on the implementation of `OnUpdate` in your views. If you do it the way Microsoft intended, it will return right away without doing any real work, thus not blocking anything - but that's entirely up to you. It's quite easy to do it wrong and get significant delays. – Mark Ransom Jan 24 '15 at 04:56
  • I agree, if OnUpdate is written the way Microsoft intended, which is hopefully the typical case, then UpdateAllViews should do very little work and feel about as blocking as send(fd,&buf,bufsz,0) with empty buffers. In the untypical case it is not nonblocking like send(fd,&buf,bufsz,MSG_DONTWAIT) or a promise/callback/queue interface. Users should not expect UpdateAllViews to work like the latter after not following best practices for OnUpdate. – jla Jan 26 '15 at 21:12