Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about Refreshing the UI in WPF

I have heard that this refreshes the UI but why so? Dispatcher invokes this empty action and thats it there is no call InvalidateMeasure() which would trigger the UI to re-measure and re-arrange and re-render inside the action. Where is here the measure and arrange process to update/refresh the 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 why is UI rendered. Answers like well that triggers ui update are not helping me any further guys.

like image 574
snowy hedgehog Avatar asked Oct 04 '22 19:10

snowy hedgehog


1 Answers

Microsoft recommends a slighty 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 halts the program while pumping the Windows message queue and handling the messages in the queue. This includes any rendering messages such as WM_PAINT and WM_NCPAINT.

The code instructs the Dispatcher to halt the current thread and pump its queue and handle all messages that have a priority of DispatcherPriority.Render or higher. If there are any pending rendering messages in the queue, for example if InvalidateRect has been called somewhere by the framework, these messages will be handled and the UI will be re-rendered.

DoEvents has always been considered a code smell because it bypasses the message queue. If you want a responsive user interface you should use worker threads instead.

like image 129
Jakob Christensen Avatar answered Oct 10 '22 21:10

Jakob Christensen