使用UI线程的BackGroundWorker线程问题 [英] BackGroundWorker Thread issue with UI Thread

查看:127
本文介绍了使用UI线程的BackGroundWorker线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在UI Thread中完成了一些数据库编码。但它冻结了我的UI

所以我将其更改为后台工作线程如



 _ MyWoker =  new  BackgroundWorker(); 
_MyWoker.DoWork + = new DoWorkEventHandler(_MyWoker_DoWork);
_MyWoker.RunWorkerCompleted + = new RunWorkerCompletedEventHandler(_MyWoker_RunWorkerCompleted);





_MyWoker_DoWork 接受我的LoadDetails();这个包含UI控件语句的函数



如何自己的UI线程来自BackGroundWorker线程?

当前例外:

< b>调用线程无法访问此对象,因为另一个线程拥有它< / b>



提前谢谢!

解决方案

您只能从UI线程 - 主线程访问UI控件。如果你尝试从任何其他线程,你会得到你见过的例外。



有两种方法可以绕过它:

1)使用Control.Invoke将操作移回UI线程。

2)在UI线程上处理的ProgressChanged事件中执行控件更新。


由于多种原因,不允许某些跨线程调用。您可以轻松地将一些方法委托给UI线程。这样,您可以在UI线程中创建一个对象,并在UI线程中调用其所有方法,但是在其他一些线程中会触发调用。当您调用与当前运行的UI相关的方法/属性时就是这种情况 - 所有这些调用都应该只在UI线程中完成。你需要使用方法调用 BeginInvoke System.Windows.Threading.Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表格)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()vs。Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何在vb.net中的其他线程上运行keydown事件 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



由方式,如果您需要将一些调用委托给非UI线程,则需要以特殊方式对该线程进行编程。如果您看到我的文章,您可以了解它的工作原理:简单阻止队列线程通信和线程间调用 [ ^ ]。



这篇小文章包含完整的源代码和详细的使用示例。请注意,从.NET v.4.0开始,您还可以使用 System.Collections.Concurrent.BlockingCollection< T>

http://msdn.microsoft.com/en-us/library/dd267312.aspx [ ^ ]。



另请参阅上面引用的文章的替代方法。我的文章的好处是我解释它是如何工作和使用的,但Microsoft类的性能更好。



-SA

Hi all,

im done some database coding in UI Thread. but its freezing my UI
so i changed that into Background worker thread like

_MyWoker = new BackgroundWorker();
_MyWoker.DoWork += new DoWorkEventHandler(_MyWoker_DoWork);
_MyWoker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_MyWoker_RunWorkerCompleted);



_MyWoker_DoWork takes my LoadDetails(); function this contain the UI controls statement

how can own UI thread From BackGroundWorker thread?
Current Exception:
<b>The calling thread cannot access this object because a different thread owns it</b>

Thanks in advance!

解决方案

You can only access UI controls from the UI thread - the main thread. If you try from any other thread, you will get the exception you have seen.

There are two ways to get round it:
1) Use Control.Invoke to move the action back to the UI thread.
2) Do the control update in the ProgressChanged event, which is handled on the UI thread.


Certain cross-threads calls are not allowed, by a number of reasons. You can easily delegate some methods to UI thread. This way, you can create an object in UI thread, and call all its methods in UI thread, but the calls are triggered in some other thread. This is the case when you call methods/properties related to the currently running UI — all such calls should be done in UI thread only. you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

By the way, if you needed to delegate some calls to non-UI thread, you would need to program that thread in a special way. You can understand how it works if you see my article: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

This small article is complete with full source code and detailed usage sample. Note that starting with .NET v.4.0, you could also use System.Collections.Concurrent.BlockingCollection<T>:
http://msdn.microsoft.com/en-us/library/dd267312.aspx[^].

See also the alternative to my article referenced above. The benefit of my article is that I explain how it works under the hood and the usage, but Microsoft class is better in performance.

—SA


这篇关于使用UI线程的BackGroundWorker线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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