了解分派器队列 [英] Understanding the Dispatcher Queue

查看:97
本文介绍了了解分派器队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我需要一些帮助来了解 Dispatcher Queue .

I think I need some help understanding the Dispatcher Queue.

新工作到达时,将其添加到调度程序队列的开头,而Dispatcher要处理工作项时,将其从开头删除.

When new work arrives it gets added at the beginning of the dispatcher queue and when the Dispatcher wants to process a working item it gets removed from the beginning.

更笼统地说:如果有工作,则将其以FIFO的方式存储在队列中,并在没有工作的情况下进行处理.

In more general terms: If there is work it gets stored in a FIFO manner inside the queue and processed as long there is no work left.

此处是MSDN文档到loopframe:

The MSDN documentation here is referring to a loop and a frame:

The Dispatcher processes the work item queue in a loop. The loop is referred to as a frame.

但是在这种情况下循环在哪里?对我来说,循环是对某事进行迭代的东西,当到达终点时,它又重新开始.

But where is a loop in this context ? For me a loop is something that iterates over something and when it reaches the end it starts over again.

frame的概念是什么?根据MSDN文档,框架是队列中工作项的重头戏吗?如果是这样,应该如何使用静态方法Disptatcher.PushFrame()?

And what's the concept of a frame ? According to the MSDN documentation a frame is a punch of working items inside the queue ? If that's true how should the static method Disptatcher.PushFrame() be used ?

最有趣的问题是,是否有任何方法可以获取队列的当前状态,尤其是队列中有多少项.

And the most interesting question is whether there is any way to get the current state of the queue especially how many items are in the queue.

它是否成立,如果执行了之前已调用的方法(并因此放入Dispatcher队列中),然后立即将其从队列中删除,或者该方法在内部持续了一段时间?

Does it hold if a method that has been invoked before (and therefor put into the Dispatcher queue) gets executed that it is then removed from the queue immediately or does it last inside for another period of time ?

我知道,这么多问题:-)

I know, So many questions :-)

推荐答案

关于Dispatcher的文档很少,因此您必须进行一些反汇编以了解其内部工作原理.

There's very little documentation surrounding the Dispatcher, so you'll have to disassemble around a bit to know about the inner workings.

基本上,调度程序是围绕应用程序的消息泵执行的工作.该问题位于 Windows消息循环上.

A dispatcher is basically something which performs work around the application's Message Pump. The one in question sits on top of the windows message loop.

因此,只能有一个应用程序Dispatcher-Application.Current.Dispatcher可以访问的全局调度程序对象.通过访问Dispatcher.CurrentDispatcher可以实现其他调度程序,根据文档

As a consequence, there can only be one application Dispatcher - the global dispatcher object accessible by Application.Current.Dispatcher. Other dispatchers are possible by accessing Dispatcher.CurrentDispatcher, which according to the documentation

获取当前正在执行的线程的Dispatcher并创建一个 新的Dispatcher(如果尚未与该线程关联).

Gets the Dispatcher for the thread currently executing and creates a new Dispatcher if one is not already associated with the thread.

但是,在此新调度程序上调用Run将会被阻止.

However, calling Run on this new dispatcher will be blocking.

当您执行Dispatcher.PushFrame时,它将内部执行循环推入Dispatcher-这是

When you do a Dispatcher.PushFrame, it pushes an inner execution loop into the Dispatcher - that's the general idea of a frame. Anything that inherits from DispatcherObject such as DispatcherFrame will have its dispatcher set to the current one. We can verify this by looking at its constructor.

private Dispatcher _dispatcher;

protected DispatcherObject()
{
    this._dispatcher = Dispatcher.CurrentDispatcher;
}

当然,仅具有简单的事件循环是不够的-有时您需要颠覆当前事件循环以强制执行其他工作.这就是为什么您有DispatcherFrame的原因.这实际上是构成事件循环的原因.将框架推入Dispatcher时,会发生以下情况:

Of course, having a simple event loop isn't enough - there are times when you need to subvert the current event loop to force other work to be done. And that's why you have a DispatcherFrame. This is what actually constitutes the event loop. When you push a frame into the Dispatcher, this is what happens:

while (frame.Continue)
        {
            if (!this.GetMessage(ref msg, IntPtr.Zero, 0, 0))
            {
                break;
            }
            this.TranslateAndDispatchMessage(ref msg);
        }

TranslateAndDispatchMessage中,消息被取出后,才对Dispatcher中的优先队列进行评估.

It is in the TranslateAndDispatchMessage that the prioritized queue in the Dispatcher gets evaluated, after a message is taken out.

如果某个操作需要很长时间才能在调度程序上运行,它将暂时停止事件循环,并且由于它不响应信号,因此应用程序似乎停止了响应.

If an operation takes a long time to run on the dispatcher, it temporarily stops the event loop, and because it doesn't respond to signalling, the application seems like it stops responding.

这是一篇文章,它使用框架强制UI通过允许事件循环很快运行来进行响应.

Here's an article which uses a frame to force the UI to respond by allowing the event loop to run shortly.

就访问队列而言,实际上无法知道分派器外部队列的状态.这是内部细节,因此没有公开是很合理的.

As for accessing the queue, as it is, there is no way to know the state of the queue outside of the Dispatcher. This is an internal detail, and it's reasonable that it's not exposed.

这篇关于了解分派器队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆