WPF DispatcherFrame 魔法 - 它是如何以及为什么起作用的? [英] WPF DispatcherFrame magic - how and why this works?
问题描述
我试图在 WPF
中制作一些动画,并在动画完成时运行一些其他操作.
I was trying to animate some stuff in WPF
and run some other operations when animation finishes.
另外,想避免动画完成回调机制,所以,我想出了一个解决方案,如下代码所示:
Also, wanted to avoid animation finish callback mechanism, so, I came up with a solution illustrated in the code below:
// Start one second of animation
...
// Pause for one second
Wait(this.Dispatcher, 1000);
// Continue and do some other stuff
...
现在,有趣的部分是 Wait
方法,它神奇地使我的代码中的阻塞暂停但动画和 UI 保持正常、响应:
Now, the interesting part is Wait
method which magically makes blocking pause in my code but the animation and UI stays normal, responsive:
public static void Wait(Dispatcher Dispatcher, int Milliseconds)
{
var Frame = new DispatcherFrame();
ThreadPool.QueueUserWorkItem(State =>
{
Thread.Sleep(Milliseconds);
Frame.Continue = false;
});
Dispatcher.PushFrame(Frame);
}
我已经阅读了关于 DispatcherFrame
的文档和几篇文章,但我仍然无法弄清楚幕后真正发生了什么,我需要一些关于如何使用 进行构造的说明PushFrame
确实有效.
I have read a documentation and a few articles about DispatcherFrame
but I am still unable to figure out what is really happening under the hood, and I need some clarification about how this construction with PushFrame
really works.
推荐答案
来自 MSDN:
推送框架
进入执行循环.
只要其 Continue
属性为 true
,该循环(即 DispatcherFrame
)就会执行.
That loop (i.e. the DispatcherFrame
) executes as long as its Continue
property is true
.
一旦 Continue
属性变为 false
,循环/帧就会退出,并且 Dispatcher 返回到它在调用 PushFrame 之前执行的循环/帧.
As soon as the Continue
property goes to false
, the loop/frame is exited and the Dispatcher returns to the loop/frame that it executed before you called PushFrame.
这篇关于WPF DispatcherFrame 魔法 - 它是如何以及为什么起作用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!