[英]Thread needs to use Invoke method to access UI controls but BackGroundworker doesn't. Why?
BackgroundWorker can access UI controls directly, but Thread 's cannot, Why? BackgroundWorker可以直接访问UI控件,但是Thread不能,为什么?
Isn't BackgroundWorker a thread? BackgroundWorker不是线程吗? If it isn't, what is it?
如果不是,那是什么? Also, why is direct accessing restricted?
另外,为什么直接访问受到限制? Is restricting direct access Microsoft's choosen way for doing something or must it be done this way?
是限制直接访问Microsoft选择做某事的方式还是必须以这种方式完成?
According to MSDN, "The BackgroundWorker
class allows you to run an operation on a separate, dedicated thread." 根据MSDN,“
BackgroundWorker
类允许您在单独的专用线程上运行操作。”
BackgroundWorker
acutally uses Form.Invoke()
to switch control to the main UI thread to report progress. BackgroundWorker
Form.Invoke()
使用Form.Invoke()
将控件切换到主UI线程以报告进度。 In the fact BackgroundWorker
is just a helper class that you could easily write yourself, it uses a second thread to do the work but then it switches to the main UI thread to access GUI. 实际上,
BackgroundWorker
只是一个您可以轻松编写自己的帮助程序类,它使用第二个线程来完成工作,但是随后切换到主UI线程来访问GUI。
Also from MSDN: "You can listen for events that report the progress of your operation and signal when your operation is completed." 同样从MSDN:“您可以侦听报告操作进度的事件,并在操作完成时发出信号。” This means that the background operation runs on a separate thread and you can't access GUI from that thread.
这意味着后台操作在单独的线程上运行,并且您无法从该线程访问GUI。 But you can access GUI controls from progress event because it is raised on the main UI thread.
但是您可以从progress事件访问GUI控件,因为它是在主UI线程上引发的。 Whenever you call
BackgroundWorker.ReportProgress()
on the worker thread, it raises BackgroundWorker.ProgressChanged
event on the main thread. 每当在工作线程上调用
BackgroundWorker.ReportProgress()
时,都会在主线程上引发BackgroundWorker.ProgressChanged
事件。
Also, you ask why GUI cannot be accessed from other threads. 另外,您问为什么无法从其他线程访问GUI。 It is because whole Windows GUI subsystem is not thread safe.
这是因为整个Windows GUI子系统都不是线程安全的。 As far as I know this is pretty normal in other platforms (outside Windows) as well, so it definitely isn't Microsoft specific.
据我所知,这在其他平台(Windows之外)中也很正常,因此它绝对不是Microsoft专用的。 It is probably from historical reasons and probably has something to do with code efficiency.
这可能是由于历史原因,可能与代码效率有关。 (A thread safe GUI would be slower.) But I can't provide any strong information source as a reference.
(线程安全的GUI会更慢。)但是我无法提供任何强大的信息来源作为参考。
A background worker will throw a cross-thread exception just as a new thread would. 后台工作人员将像新线程一样抛出跨线程异常。 A backgroundworker is just a fancy way to start a new thread.
后台工作人员只是启动新线程的一种理想方法。
If you are using the ProgressChanged event then you do not need to invoke it because there is some .NET magic behind the scenes doing it for you. 如果您使用的是ProgressChanged事件,则无需调用它,因为幕后有一些.NET魔术可以为您做这件事。
More information on background workers: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.100).aspx 有关后台工作者的更多信息: http : //msdn.microsoft.com/zh-cn/library/system.componentmodel.backgroundworker(v=vs.100).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.