WPF Dispatcher查询 [英] WPF Dispatcher Queries

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

问题描述

您好,



我已经研究了调度员的全部内容,但我有一些疑问,我在一些采访中被问到并想要验证我是否是错误或正确。



1)我们可以在运行时在WPF的不同线程中创建一个ui元素吗?我不清楚这个问题。第一个问题是我们可以在不同的线程中创建UI元素。如果是,那么调度员方案是什么呢。

2)我们可以在WPF应用程序中拥有多个调度程序吗? 我认为它是NO

3)WPF应用程序可以有多个UI线程吗? 我认为没有

4)如果我们在WPF中使用Form.Show()启动表单,它会启动另一个调度程序吗? 再次否

推荐答案

以下是我的答案,我试图纠正或澄清解决方案1的答案:



Here are my answers, where I try to correct or clarify the answers of Solution 1:

  1. 这个问题比看上去更加微妙。解决方案1的答案简化了事情。实际上,您可以在非UI线程中创建UI元素,即调用构造函数,更改某些属性等。您不能做的是将这些元素插入在不同线程中运行的UI中。为此,您需要通过调用 BeginInvoke
  2. 使用当前调度程序错了。如果您有多个线程(不包括原始UI线程),您不仅可以使用多个调度程序,还必须拥有多个调度程序。原因如下:您使用当前的调度程序实例使用静态属性 System.Windows.Threading.Dispatcher.CurrentDispatcher

    https://msdn.microsoft.com/en-us /library/system.windows.threading.dispatcher.currentdispatcher%28v=vs.110%29.aspx [ ^ ]。



    阅读此MSDN帮助文档。您将看到调度程序的实例是延迟初始化的主题。每个不同的线程是不同的实例吗?是的,一点没错。我检查了一下,你也可以通过拥有多个线程来检查它,获取调度程序实例并将它们作为参考标识进行比较。 他们是不同的例子;你别无选择。
  3. 是的,你可以使用不同的UI线程,但这是不切实际的。当您调用 Application.Run 时,可以在新线程中启动新UI。您可以在不同的线程中使用不同的应用程没有必要这样做。解决方案1错误地声称这是创建单独的调度程序实例的方法;但实际上,即使您只有一个UI线程,调度程序实例也是分开的;它足以为每个非UI线程创建调度程序实例,正如我在前面的项目中所解释的那样。关于模态对话行为的观点在解决方案1中非常好。实际结论:只使用一个UI线程。



    还有一个方面我们没有提到:使用WPF时会隐式创建一些额外的线程。例如,除了UI线程本身之外,还创建了一个单独的渲染线程。文件对话框会导致创建和执行额外的线程;它们用于跟踪由其他进程执行的文件系统的更改。
  4. Form.Show()与WPF无关。但是有一个微妙的时刻:您可以在同一个应用程序中运行两个不同的UI实例。我已经解释过了,但是你可以做的是:在一个线程中运行WPF Application.Run 并且 System.Windows.Forms.Application.Run 在另一个。



    令人惊讶的是,它有效;我试着用纯粹的实验目的。但在实践中,它几乎没有任何意义。顺便说一句,你可以在WPF中托管 System.Windows.Forms.Control 。请参阅: https://msdn.microsoft.com/en -us / library / vstudio / ms751761%28v = vs.100%29.aspx [ ^ ]。
  1. This question is more delicate than it seems. The answer of Solution 1 simplifies things. Actually, you can create an UI element in a non-UI thread, that is, call a constructor, change some properties, etc. What you cannot do is inserting these element in the UI running in a different thread. To do so, you would need to use the current dispatcher via Invoke or BeginInvoke
  2. You are wrong. Not only you can use more than one dispatcher, you are bound to have more than one, if you have more then one thread (not counting original UI thread). Here is why: you use the current dispatcher instance using the static property System.Windows.Threading.Dispatcher.CurrentDispatcher:
    https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.currentdispatcher%28v=vs.110%29.aspx[^].

    Read this MSDN help document. You will see that the instance of dispatcher is a subject of lazy initialization. Is it different instance for each of different threads? Yes, absolutely. I checked it up, and you can check it up, too, by having more then one threads, taking the dispatcher instances and comparing them for referential identity. They are different instances; you have no choice.
  3. Yes, you can use different UI threads, but this would be impractical. You start new UI in new thread when you call Application.Run. You can have different applications in different threads. There is no need to do so. Solution 1 mistakenly claims that this is the way to create separate dispatcher instances; but in reality, dispatcher instances are separate even if you have only one UI thread; it's enough to create dispatcher instances for each non-UI thread, as I explained in previous item. The point about the modal dialog behavior is really good in Solution 1 though. The practical conclusion: use only one UI thread.

    There is a remaining aspect which we did not mention: some extra threads are implicitly created when you use WPF. For example, in addition to UI thread itself, a separate "rendering thread" is created in addition to it. File dialogs causes extra threads to be created and executed; they are used to track changes in file systems performed by other processes.
  4. Form.Show() is irrelevant to WPF. But there is a delicate moment: you can run two different UI instances in the same application. I already explained it, but here is what you can do: run WPF Application.Run in one thread and System.Windows.Forms.Application.Run in another.

    Amazingly, it works; I tried it with pure experimental purposes. But in practice it makes little to no sense. By the way, you can host System.Windows.Forms.Control in WPF. Please see: https://msdn.microsoft.com/en-us/library/vstudio/ms751761%28v=vs.100%29.aspx[^].





-SA


看看线程模型



Take a look at Threading Model

引用:

1)我们可以在运行时在WPF的不同线程中创建一个ui元素吗?我不清楚这个问题。第一个问题是我们可以在不同的线程中创建UI元素。如果是,那么调度员场景是什么呢。

1) Can we create a ui element at runtime in a different thread in WPF? I am not clear about this question. The first question would be can we create UI elements in a different thread. If yes what would be the dispatcher scenario then.





从历史上看,Windows允许UI元素只能由创建它们的线程访问。这意味着负责某个长时间运行任务的后台线程在完成时无法更新文本框。 Windows这样做是为了确保UI组件的完整性。如果在绘画期间内容由后台线程更新,列表框可​​能看起来很奇怪。





Historically, Windows allows UI elements to be accessed only by the thread that created them. This means that a background thread in charge of some long-running task cannot update a text box when it is finished. Windows does this to ensure the integrity of UI components. A list box could look strange if its contents were updated by a background thread during painting.

Quote:

2)我们可以在WPF应用程序中拥有多个调度程序吗?我认为它是NO

2) Can we have more than one dispatchers in WPF Application? I think its a NO





是的,我们可以:每个UI线程必须至少有一个Dispatcher,每个Dispatcher只能执行一个Dispatcher线程。





Yes, we can: Every UI thread must have at least one Dispatcher, and each Dispatcher can execute work items in exactly one thread.

Quote:

3)WPF应用程序可以有多个UI线程?我想不是

3) Can a WPF Application have multiple UI Threads? I think NO





来自 WPF可以/有多个GUI线程吗?



可以有多个GUI线程(因此,多个Dispatcher实例。

但是:只需创建一个新窗口(模态或非模态)不会创建新的GUI线程。一个人需要显式创建线程(通过创建一个新的Thread实例)。

注意:使用Dispatcher.PushFrame()来阻止调用者,而不是使用单独的线程,可能会实现模态对话框。仍然允许调度事件的方法。





From Can/Does WPF have multiple GUI threads?

There can be multiple GUI threads (and therefor multiple Dispatcher instances).
However: Simply creating a new window (modal or not) does not create a new GUI thread. One needs to create the thread explicitly (by creating a new instance of Thread).
Note: Instead of using separate threads, modal dialogs are likely being realized by using Dispatcher.PushFrame() which blocks the caller of this method while still allowing events to be dispatched.

Quote:

4 )如果我们在WPF中使用Form.Show()启动表单,它会启动另一个调度程序吗?再次否

4) If we are launching the form using Form.Show() in WPF, will it launch another dispatcher? Again No



见上文3)


See 3) above


这篇关于WPF Dispatcher查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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