gpt4 book ai didi

wpf - DoEvents : Dispatcher. 调用与 PushFrame

转载 作者:行者123 更新时间:2023-12-03 14:30:26 24 4
gpt4 key购买 nike

最近我发现了一种方法,它可以执行调度程序队列中的所有待处理消息,直到指定优先级。我之前已经有这样的代码,但它们使用完全不同的方法。这是他们两个:

PushFrame方式:

/// <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;
}

来源: MSDN Library

阻塞调用方式:
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);
}

来源: Blog

哪个更好,两种解决方案之间是否存在任何功能差异?

更新:这是内联代表的第二个,与第一个一样,使其更短:
/// <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);
}

最佳答案

你有点回答了你自己的问题。您选择哪个并不重要,因为两者在后台执行相同的操作。

两者都遇到了这个:

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

但是 PushFrame 更好一些,因为您不需要创建空委托(delegate)。

关于wpf - DoEvents : Dispatcher. 调用与 PushFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25442018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com