确保事情在WPF UI线程上运行 [英] Ensuring that things run on the UI thread in WPF

查看:261
本文介绍了确保事情在WPF UI线程上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个WPF应用程序。我做与服务器端的一些异步通信,我用事件汇总与棱镜在客户端上。这两种东西的结果在新线程催生这是不UI线程。如果我试图做这些回调和事件处理的线程的世界将土崩瓦解,它现在已经开始做WPF操作。

I'm building a WPF application. I'm doing some async communication with the server side, and I use event aggregation with Prism on the client. Both these things results in new threads to be spawned which are not the UI thread. If I attempt to do "WPF operations" on these callback and event handler threads the world will fall apart, which it now has started doing.

首先,我遇到了试图建立在从服务器回调一些WPF对象的问题。我被告知,该线程需要在STA模式下运行。现在,我想更新一个棱镜事件处理一些UI数据,并告诉我说:

First I met problems trying to create some WPF objects in the callback from server. I was told that the thread needed to run in STA mode. Now I'm trying to update some UI data in a Prism event handler, and I'm told that:

调用方无法访问该线程,因为不同的线程拥有它。

The caller cannot access this thread because a different thread owns it.

所以, 什么是关键,以获得正确的事情在WPF?我在的这个MSDN帖子。我开始得到它,但我没有精灵呢。

So; what's the key to getting things right in WPF? I've read up on the WPF Dispatcher in this MSDN post. I'm starting to get it, but I'm no wizard yet.

  1. 是始终使用Dispatcher.Invoke当我需要运行一些东西,我不知道将被称为UI线程上的关键?
  2. 在它的问题,如果它实际上是被称为UI线程,而我呢Dispatcher.Invoke呢?
  3. Dispatcher.Invoke =同步。 Dispathcher.BeginInvoke =异步?
  4. 威尔Dispatcher.Invoke请求UI线程,然后停下来等待呢?它是不好的做法并不太敏感项目的风险?
  5. 如何让调度员呢?将Dispatcher.CurrentDispatcher总是给我分派再presenting UI线程?
  6. 会存在多个调度,或者是调度员基本相同应用程序的UI线程?
  7. ,什么是成交与BackgroundWorker的?当我用这个来代替?我认为这始终是异步?
  8. 在该UI线程上运行(由被调用)的STA公寓模式下运行将一切吗?即如果我有东西,需要将在STA模式下运行 - 将Dispatcher.Invoke是否足够?

任何人想clearify的东西给我吗?任何有关建议等?谢谢!

Anyone wanna clearify things for me? Any related recommendations, etc? Thanks!

推荐答案

去在你的每一个问题,一个接一个:

Going over each of your questions, one by one:

  1. 在不太;你应该只调用到UI线程需要的时候。见#2。
  2. 是的,这非常重要。你不应该只是自动调用的一切。关键是,如果有必要,只引用到UI线程。要做到这一点,你可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.checkaccess.aspx">Dispatcher.CheckAccess方法
  3. 这是正确的。
  4. 也是正确的,是的,你跑不那么敏感程序的风险。如有必要,在大多数情况下,你是不是要寻找一个严重的性能(我们正在谈论毫秒上下文切换),但只应调用 。话虽这么说,在某些点上是不可避免的,所以,不,我不会说这是不好的做法的。这仅仅是一个解决方案,你会偶尔遇到的每一个问题。
  5. 在我所看到的每一种情况下,我曾与 Dispatcher.CurrentDispatcher 作出了应有的。对于复杂的情况下,这可能是不够的,但我(个人)还没有看到他们。
  6. 在不完全正确的,但这种思路也不会做任何伤害。让我这样说吧:在调度可以用来获得的UI线程应用程序。但它并不在其本身的UI线程。
  7. 的BackgroundWorker 一般用来当你有一个非常耗时的操作,并希望同时运行在后台的运行维护一个负责任的UI。通常情况下你不使用的BackgroundWorker的调用而不是的,而是你在的调用一起使用的BackgroundWorker的。也就是说,如果你需要在你的BackgroundWorker更新一些UI对象,你可以调用到UI线程,执行更新,然后返回到原来的操作。
  8. 是。 WPF应用程序的UI线程,顾名思义,必须在单线程运行。
  1. Not quite; you should only invoke onto the UI thread when necessary. See #2.
  2. Yes, it does matter. You should not just automatically Invoke everything. The key is to only invoke onto the UI thread if necessary. To do this, you can use the Dispatcher.CheckAccess method.
  3. That is correct.
  4. Also correct, and yes, you do run the risk of less responsive programs. Most of the time, you are not going to be looking at a severe performance hit (we're talking about milliseconds for a context switch), but you should only Invoke if necessary. That being said, at some points it is unavoidable, so no, I would not say it is bad practice at all. It is just one solution to a problem that you will encounter every now and then.
  5. In every case I have seen, I have made due with Dispatcher.CurrentDispatcher. For complex scenarios, this may not be sufficient, but I (personally) have not seen them.
  6. Not entirely correct, but this line of thinking will not do any harm. Let me put it this way: the Dispatcher can be used to gain access to the UI thread for the application. But it is not in and of itself the UI thread.
  7. BackgroundWorker is generally used when you have a time-consuming operation and want to maintain a responsive UI while running that operation in the background. Normally you do not use BackgroundWorker instead of Invoke, rather, you use BackgroundWorker in conjunction with Invoke. That is, if you need to update some UI object in your BackgroundWorker, you can Invoke onto the UI thread, perform the update, and then return to the original operation.
  8. Yes. The UI thread of a WPF application, by definition, must be running in a single-threaded apartment.

还有很多可说的对的BackgroundWorker ,我敢肯定,很多问题都已经专门为它,所以我不会去考虑太多的深度。如果你好奇,请查看 MSDN页的BackgroundWorker类

There's a lot to be said about BackgroundWorker, I'm sure many questions are already devoted to it, so I won't go into too much depth. If you're curious, check out the MSDN page for BackgroundWorker class.

这篇关于确保事情在WPF UI线程上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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