5

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.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
snowy hedgehog
  • 652
  • 1
  • 7
  • 23
  • It would probably be helpful if you'd post a reference to the source of this idea. – Clemens Mar 06 '13 at 13:40
  • I edited my question a bit. There is no reference what whatever you mean. This seems is a common way to refresh a control in wpf. If you are familiar with WPF you wont need any references to understand the code. I would like to know why is this code refreshing the UI since there is no action or no action which would let the UI do the remeasuing and re-rendering. Its empty action but seems to refresh ui. Why? – snowy hedgehog Mar 06 '13 at 15:26
  • When you write "I have heard that this refreshes the UI" or "This seems is a common way to refresh a control in wpf" it sounds like you got this idea from somewhere. Would be interesting to know from where exactly... – Clemens Mar 06 '13 at 15:47
  • @Clemens I would guess here http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx - it matches the code above at least - although I've seen the idea in many places. – davisoa Mar 06 '13 at 15:53
  • @davisoa Thanks for the link, I guess it already contains the answer to this question. I'm still wondering in what exact situations this approach would be better than simply calling UIElement.UpdateLayout. – Clemens Mar 06 '13 at 16:41
  • @Clemens put my code into google and you will find the article. so easy to figure it out dude. anyways, the article sucks, nothing is explanined in details. i want to know the details. – snowy hedgehog Mar 06 '13 at 16:47
  • "put my code into google and you will find the article". @davisoa already did that, thanks again for taking the effort. – Clemens Mar 06 '13 at 16:54
  • there are similar codes to this one.. all do the same and i would like to know whats happening in the background, behind the hood. If you dont know why, please stop turning this into a discussion. if you want to discuss anything similar to this, create a chat room. I ll join your chat gladly – snowy hedgehog Mar 06 '13 at 16:59

2 Answers2

3

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.

Jakob Christensen
  • 14,826
  • 2
  • 51
  • 81
1

This can be used to force WPF to update the UI because of the priority of the invocation. Since this invoke uses the Dispatcher synchronously, any tasks that are already queued with an equal or higher priority than DispatcherPriority.Render will be run before running the delegate you provided.

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

davisoa
  • 5,407
  • 1
  • 28
  • 34
  • I still dont see why would this update the UI? Fine it waits till taks with priority "send","normal" are done and then the empty action will be executed and thats it. There si no re-measuing. Where is the re-measuing, re-arranging and re-rendering to update the UI? – snowy hedgehog Mar 06 '13 at 15:31
  • I believe the `Render` priority includes tasks that perform the measuring, arranging, and of course the rendering - I'm looking for something that defines what happens here now. – davisoa Mar 06 '13 at 15:37
  • Render is just the priority and it does not include any extra tasks. – snowy hedgehog Mar 06 '13 at 15:45
  • Render is the priority of the task you are adding, but the Dispatcher has Render tasks of it's own. These other tasks that are already in the queue are being executed, causing the UI to update. – davisoa Mar 06 '13 at 15:47
  • And what if Dispatcher doesnt have any current Render tasks of its own. What if all is already rendered and there is nothing more to do, then Dispatcher would be empty. When dispatcher is empty and you isert a empty task, what then? – snowy hedgehog Mar 06 '13 at 16:45
  • Would you please remove your question. Its not helping anyone here. Everybody knows those general things about Dispatcher but i am looking for details why things happen. :) – snowy hedgehog Mar 06 '13 at 16:48