DoEvents: Dispatcher.Invoke vs. PushFrame

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:

/// <summary>
/// Enters the message loop to process all pending messages down to the specified
/// priority. This method returns after all messages have been processed.
/// </summary>
/// <param name="priority">Minimum priority of the messages to process.</param>
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 { };

/// <summary>
/// Processes all pending messages down to the specified priority.
/// This method returns after all messages have been processed.
/// </summary>
/// <param name="priority">Minimum priority of the messages to process.</param>
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:

/// <summary>
/// Processes all pending messages down to the specified priority.
/// This method returns after all messages have been processed.
/// </summary>
/// <param name="priority">Minimum priority of the messages to process.</param>
public static void DoEvents2(
    DispatcherPriority priority = DispatcherPriority.Background)
{
    Dispatcher.CurrentDispatcher.Invoke(new Action(delegate { }), priority);
}
+4
source share
1 answer

. , , .

:

While (dispatcherFrame.Continue)
{
  Dispatcher.GetMessage();
  Dispatcher.TranslateAndDispatch();
}

PushFrame , .

+3

All Articles