为什么不能从一个BackgroundWorker UI组件进行访问? [英] Why can't UI components be accessed from a backgroundworker?

查看:148
本文介绍了为什么不能从一个BackgroundWorker UI组件进行访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题的所有共享资源。这是围绕多线程操作的整个问题。

Threads all share resources. That's the whole problem around multi-threaded operations.

MSDN 说:

您一定要小心,不要操纵你的DoWork的事件>处理任何用户界面对象。相反,通信,以通过ProgressChanged和RunWorkerCompleted事件用户界面

You must be careful not to manipulate any user-interface objects in your DoWork event >handler. Instead, communicate to the user interface through the ProgressChanged and RunWorkerCompleted events.

BackgroundWorker的事件不能跨应用程序域边界封送处理。不要使用BackgroundWorker组件在一个以上的应用程序域进行多线程操作。

BackgroundWorker events are not marshaled across AppDomain boundaries. Do not use a BackgroundWorker component to perform multithreaded operations in more than one AppDomain.

然而,当我使用BackgroundWorker的,这并不是说我要小心,不要操作任何UI对象,那就是can't_如果我试图从DoWork的事件访问UI组件。在code编译,但是code的DoWork的运行时,我得到一个错误:

And yet when I use the backgroundworker, it's not that I need to be careful not to manipulate any UI objects, it's that can't_ if I try to access the UI components from the DOWork event. The code compiles, but when the code for the DoWork runs, I get an error:

跨线程操作无效:控制'utAlerts'从一个线程比它创建于其他线程访问

Cross-thread operation not valid: Control 'utAlerts' accessed from a thread other than the thread it was created on.

MSDN并没有说明这是怎么做的,或者为什么事情。装饰有一些属性是prevents这BackgroundWorker的?这是如何实现的呢?

MSDN doesn't say anything about how is this done or why. Is the backgroundworker decorated with some attribute that prevents this? How is this accomplished?

推荐答案

如果你处理程序是在你的UI类的实例方法,你应该能够访问该类的成员。

If you're handler is an instance method in your UI class, you should have access to the members of that class.

这是我的应用程序,甚至不会编译,如果我试图从DoWork的事件访问UI组件。

it's that my app won't even compile if I try to access the UI components from the DOWork event.

这只会发生,如果你的的DoWork 的处理程序是静态的还是在不同的班级比UI组件。在这种情况下,你可能无法访问他们,因为他们是不可见的给你。

This will only happen if your DoWork handler is static or in a different class than the UI components. In that case, you may not have access to them, as they are not visible to you.

编辑:

BackgroundWorker的目的是做工作是无关的用户界面。你不能在除UI线程以外的任何线程改变用户界面元素,为用户界面元素往往有线程关联。这实际上无关与BackgroundWorker的,而是线程和用户界面元素。

BackgroundWorker is intended to do "work" that is unrelated to your User Interface. You cannot change User Interface elements on any thread other than the UI thread, as user interface elements tend to have thread affinity. This actually has nothing to do with BackgroundWorker, but rather threading and user interface elements.

BW旨在解决这个给你进度和完成时自动整理返回到UI线程为您的活动,让你改变UI元素存在。但是,你可以随时通过 Control.Invoke 在Windows窗体或 Dispatcher.Invoke 在WPF直接这样做你自己。

BW is intended to work around this by giving you progress and completion events that are automatically marshalled back onto the UI thread for you, allowing you to change UI elements there. However, you can always do this directly yourself via Control.Invoke in Windows Forms or Dispatcher.Invoke in WPF.

至于它是如何工作 - 这取决于什么框架,您使用。例如,在Windows窗体,每个控制(这是所有的UI元素的基类)有一个手柄,和手柄是内部本地窗口句柄。这个句柄用于检查窗口的线程ID 针对当前线程ID。这允许进行检查,而不存储额外变量

As to how this works - It depends on what framework you're using. For example, in Windows Forms, every Control (which is the base class of all of the UI elements) has a Handle, and the Handle is internally a native window handle. This handle is used to check the window's Thread ID against the current thread ID. This allows the check to be made without storing extra variables.

这篇关于为什么不能从一个BackgroundWorker UI组件进行访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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