从一个单独的线程访问形式的控制 [英] Accessing a form's control from a separate thread
问题描述
我在练习线程和过这个问题来了。情况是这样的:
I'm practising on threading and came across this problem. The situation is like this:
-
我有下载文件,一个用于显示在单一表单上4进度条一页面加载状态等等...
I have 4 progress bars on a single form, one for downloading a file, one for showing the page loading status etc...
我要控制每个进度从一个单独的线程的进度。
I have to control the progress of each ProgressBar from a separate thread.
问题是我收到的出现InvalidOperationException 它说
跨线程操作无效:控制'progressBar1从比它创建的线程以外
A线程访问
Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.
我错了这种方法或任何人可以告诉我如何实现这一点?
Am I wrong in this approach or can anybody tell me how to implement this?
推荐答案
用户界面元素只能由UI线程访问。 WinForms和WPF / Silverlight中不允许从多个线程访问控制。
The UI elements can only be accessed by the UI thread. WinForms and WPF/Silverlight doesn't allow access to controls from multiple threads.
这篇关于从一个单独的线程访问形式的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!