Wpf DoEvents:Dispatcher.Invoke与PushFrame
最近,我发现了一个方法,该方法可以执行dispatcher队列中的所有挂起消息,直到达到指定的优先级。我以前已经有过这样的代码,但它们使用完全不同的方法。这两个都是: 推架方式:Wpf DoEvents:Dispatcher.Invoke与PushFrame,wpf,dispatcher,Wpf,Dispatcher,最近,我发现了一个方法,该方法可以执行dispatcher队列中的所有挂起消息,直到达到指定的优先级。我以前已经有过这样的代码,但它们使用完全不同的方法。这两个都是: 推架方式: /// <summary> /// Enters the message loop to process all pending messages down to the specified /// priority. This method returns after all messages have
/// <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;
}
//
///进入消息循环以将所有挂起的消息向下处理到指定的时间
///优先权。此方法在处理完所有消息后返回。
///
///要处理的消息的最小优先级。
公共静态事件(
DispatcherPriority优先级=DispatcherPriority.Background)
{
DispatcherFrame=新DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(
优先,
新DispatcherOperationCallback(ExitFrame),帧);
调度员.推架(框架);
}
私有静态对象ExitFrame(对象f)
{
((DispatcherFrame)f).Continue=false;
返回null;
}
资料来源:
阻塞调用方式:
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);
}
private static Action EmptyDelegate=delegate{};
///
///按指定的优先级处理所有挂起的消息。
///此方法在处理完所有消息后返回。
///
///要处理的消息的最小优先级。
公共静态void DoEvents2(
DispatcherPriority优先级=DispatcherPriority.Background)
{
Dispatcher.CurrentDispatcher.Invoke(EmptyDelegate,优先级);
}
资料来源:
哪一种更好?两种解决方案之间是否存在功能差异
更新:这里是第二个内联委托,与第一个一样,使其更短:
/// <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);
}
//
///按指定的优先级处理所有挂起的消息。
///此方法在处理完所有消息后返回。
///
///要处理的消息的最小优先级。
公共静态void DoEvents2(
DispatcherPriority优先级=DispatcherPriority.Background)
{
调用(新操作(委托{}),优先级);
}
你回答了自己的问题。你选择哪一个并不重要,因为两者在背景中都是一样的
两者都会遇到这样的问题:
While (dispatcherFrame.Continue)
{
Dispatcher.GetMessage();
Dispatcher.TranslateAndDispatch();
}
但是,PushFrame有点好,因为您不需要创建空委托。好吧,我需要以任何一种方式创建委托,PushFrame代码总共更长。我只是拉出了静态委托实例(我的源代码已经这样做了),因为我相信保持内存分配可能会快一点。但它可能无关紧要,我也可以内联该部分,从而得到一个方法。但很高兴知道两者实际上是相同的。当使用PushFrame时,主窗口的大小和移动会出现问题。