Confusion about updating user interface in WPF

I heard this updates the interface, but why so? Dispatcher calls this empty action, and that means there is no call to InvalidateMeasure() , which forces the user interface to re-measure and re-position and re-display inside the action. Where is the measure and order of the process for update/refresh UI?

 private static Action EmptyDelegate = delegate() { }; public static void Refresh(UIElement uiElement) { uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate); } 

Any help?

EDITED: I want to know the details of why the user interface. Answers like well that triggers ui update don't help me more than others.

+4
source share
2 answers

Microsoft recommends a slightly different way of doing what your code does using PushFrame .

The code has nothing to do with rendering. What it does is similar to the DoEvents method known from VB6 and WinForms. DoEvents stops the program while pumping the Windows message queue and processing messages in the queue. This includes any rendering messages such as WM_PAINT and WM_NCPAINT .

The code tells the dispatcher to stop the current thread and pump its queue and process all messages that have DispatcherPriority.Render priority or higher. If there are pending rendering messages in the queue, for example, if InvalidateRect was called somewhere within the framework, these messages will be processed and the user interface will be displayed again.

DoEvents has always been considered a code smell because it bypasses the message queue. If you need a flexible user interface, you should use workflows instead.

+2
source

This can be used to force WPF to update the user interface due to call priority. Because this call is used by the dispatcher synchronously, all tasks that are already queued with equal or higher priority than DispatcherPriority.Render will be executed before the delegate you provide runs.

Here is a list of potential DispatcherPriority values, and it explains that order tasks are performed by the dispatcher.

+1
source

All Articles