I recently found a method that executes all pending messages in a dispatcher queue up to a specified priority. I already had this code before, but they use completely different methods. Here are both of them:
PushFrame Path:
public static void DoEvents(
DispatcherPriority priority = DispatcherPriority.Background)
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(
priority,
new DispatcherOperationCallback(ExitFrame), frame);
Dispatcher.PushFrame(frame);
}
private static object ExitFrame(object f)
{
((DispatcherFrame) f).Continue = false;
return null;
}
Source: MSDN Library
Invoke lock method:
private static Action EmptyDelegate = delegate { };
public static void DoEvents2(
DispatcherPriority priority = DispatcherPriority.Background)
{
Dispatcher.CurrentDispatcher.Invoke(EmptyDelegate, priority);
}
Source: Blog
Which is better and are there any functional differences between the two solutions?
Update: . Here is number two with the delegate indicated in the first issue, which makes it even shorter:
public static void DoEvents2(
DispatcherPriority priority = DispatcherPriority.Background)
{
Dispatcher.CurrentDispatcher.Invoke(new Action(delegate { }), priority);
}
source
share