使用 Dispatcher.Invoke 从非主线程更改 WPF 控件
我最近开始使用 WPF 进行编程,并遇到了以下问题。我不明白如何使用 Dispatcher.Invoke()
方法。我在线程方面有经验,并且制作了一些简单的 Windows 窗体程序,其中我只使用了“
Control.CheckForIllegalCrossThreadCalls = false;
是的,我知道这很蹩脚”,但这些是简单的监视应用程序。
事实上,现在我正在制作一个在后台检索数据的 WPF 应用程序,我启动一个新线程来调用以检索数据(从网络服务器),现在我想将其显示在我的 WPF 表单上。问题是,我无法从此线程设置任何控制。甚至没有标签或任何东西。如何解决这个问题?
回答评论:
@贾尔夫普:
那么当我获取数据时,我会在“新步骤”中使用这个 Dispatcher 方法吗?或者我应该让后台工作人员检索数据,将其放入一个字段中并启动一个新线程,等待该字段被填充并调用调度程序以将检索到的数据显示到控件中?
I have recently started programming in WPF and bumped into the following problem. I don't understand how to use the Dispatcher.Invoke()
method. I have experience in threading and I have made a few simple Windows Forms programs where I just used the
Control.CheckForIllegalCrossThreadCalls = false;
Yes I know that is pretty lame but these were simple monitoring applications.
The fact is now I am making a WPF application which retrieves data in the background, I start off a new thread to make the call to retrieve the data (from a webserver), now I want to display it on my WPF form. The thing is, I cannot set any control from this thread. Not even a label or anything. How can this be resolved?
Answer comments:
@Jalfp:
So I use this Dispatcher method in the 'new tread' when I get the data? Or should I make a background worker retrieve the data, put it into a field and start a new thread that waits till this field is filled and call the dispatcher to show the retrieved data into the controls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先要了解的是,Dispatcher 并非设计用于运行长时间阻塞操作(例如从 Web 服务器检索数据...)。当您想要运行将在 UI 线程上执行的操作(例如更新进度条的值)时,可以使用 Dispatcher。
您可以做的是在后台工作程序中检索数据并使用 ReportProgress 方法在 UI 线程中传播更改。
如果您确实需要直接使用 Dispatcher,那非常简单:
The first thing is to understand that, the Dispatcher is not designed to run long blocking operation (such as retrieving data from a WebServer...). You can use the Dispatcher when you want to run an operation that will be executed on the UI thread (such as updating the value of a progress bar).
What you can do is to retrieve your data in a background worker and use the ReportProgress method to propagate changes in the UI thread.
If you really need to use the Dispatcher directly, it's pretty simple:
japf已经正确回答了。如果您正在查看多行操作,可以按如下方式编写。
给其他想要了解性能的用户的信息:
如果您的代码需要编写高性能,您可以首先使用 CheckAccess 标志检查是否需要调用。
请注意,方法 CheckAccess() 在 Visual Studio 2015 中是隐藏的,因此只需编写它即可,而不必期望智能感知会显示它。请注意,CheckAccess 会产生性能开销(几纳秒的开销)。只有当您想不惜一切代价节省执行“调用”所需的微秒时,它才更好。另外,当调用方法确定它是否在 UI 线程中时,总是可以选择创建两个方法(使用调用,其他不使用)。当您应该考虑调度程序的这个方面时,这只是最罕见的情况。
japf has answer it correctly. Just in case if you are looking at multi-line actions, you can write as below.
Information for other users who want to know about performance:
If your code NEED to be written for high performance, you can first check if the invoke is required by using CheckAccess flag.
Note that method CheckAccess() is hidden from Visual Studio 2015 so just write it without expecting intellisense to show it up. Note that CheckAccess has overhead on performance (overhead in few nanoseconds). It's only better when you want to save that microsecond required to perform the 'invoke' at any cost. Also, there is always option to create two methods (on with invoke, and other without) when calling method is sure if it's in UI Thread or not. It's only rarest of rare case when you should be looking at this aspect of dispatcher.
当一个线程正在执行并且您想要执行被当前线程阻塞的主 UI 线程时,请使用以下内容:
当前线程:
主 UI 线程:
When a thread is executing and you want to execute the main UI thread which is blocked by current thread, then use the below:
current thread:
Main UI thread:
上面的@japf答案工作正常,在我的例子中,一旦CEF浏览器<,我想将鼠标光标从旋转轮更改回正常的箭头 /strong> 完成页面加载。如果它可以帮助某人,这里是代码:
The @japf answer above is working fine and in my case I wanted to change the mouse cursor from a Spinning Wheel back to the normal Arrow once the CEF Browser finished loading the page. In case it can help someone, here is the code: