集合更新和Winform线程编组 [英] Collection update and Winform thread marshalling

查看:115
本文介绍了集合更新和Winform线程编组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了几个通用绑定集合,以后可能会在WinForm应用程序中用作数据源.

我的问题是这些集合可能无法与其绑定到的控件之一被同一线程访问,这将导致跨线程异常或其他问题.
考虑到我宁愿对UI线程之外的控件也没有任何引用,因此我试图编组此线程交叉操作,因此,我既不会使用Control.Invoke方法也不会使用BeginInvoke方法.

我看到使用WPF可以通过调度程序来调用动作,但是不应将其与WinForms一起使用,因此它实际上并不适合我的情况.
因此,现在我正在寻找一种解决方案,用于封送UI线程和后台线程,以在不引用控件本身的情况下更新控件的数据源.

I implemented a couple generic binding collections that will probably be used later as datasources in a WinForm application.

My problem is that those collections may not be accessed by the same thread as the one of the control they are bounded to, this would lead to cross-thread exceptions or maybe other issues.
I am trying to marshal this thread crossing operations, considering that I would rather not have any reference to the control outside the UI thread, so I will not use neither the Control.Invoke method nor the BeginInvoke one.

I saw that with WPF it is possible to invoke actions thanks to a dispatcher, but it should not be used with WinForms so it is not really adapted to my situation.
So right now I am looking for a solution to marshal UI thread and background ones updating the datasources of controls without reference to the control itself.

推荐答案

您所说的不有任何道理. ControlDispatcher的方法InvokeBeginInvoke都在UI线程上调用委托实例,因此,这些方法的作用相同.另外,Dispatcher可以很好地用于Forms上.您可以轻松地进行检查.

如果要将实例从一个线程委托给另一个非UI线程的线程,则这些方法将不起作用.但是,您自己可以轻松创建类似的机制,但是仅对于可使用类似机制的线程而言;也就是说,这样的线程应该有一个循环,处理提交给调用的所有委托实例.我在文章用于线程通信的简单阻塞队列中详细解释了它的工作方式.和线程间调用 [ ^ ].

请注意,您不必使用我的队列类,因为您可以使用System.Collections.Concurrent.BlockingCollection<>.但是,在我的文章中附有用法示例后,您可以看到如何将其用于在不同线程上实现委托实例的实现.

另请参阅我过去的答案:
Control.Invoke()与Control.BeginInvoke()
Treeview Scanner和MD5问题
主线程上的.NET事件
如何获取keydown事件在vb.net的不同线程上运行
启用禁用+多线程后控件事件不会触发.

—SA
What you say does not make any sense. Both Control''s and Dispatcher''s methods Invoke and BeginInvoke invoke delegate instance on the UI thread, so, these methods do the same. Also, Dispatcher can well be used on Forms; you can easily check it up.

These method won''t work if you want to delegate instances from one thread to another thread which is not a UI thread. However, it''s easy to create a similar mechanism by yourself, but only for threads for which the similar mechanism is usable; that is, such thread should have a loop handling all delegate instanced submitted for invocation. I explained how it works in detail in my article Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

Note that you don''t have to use my queue class, as you can use System.Collections.Concurrent.BlockingCollection<>. But with my article complete with usage samples, you can see how to use it for implementation of the invocation of the delegate instance on a different thread.

See also my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5,
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.

—SA


这篇关于集合更新和Winform线程编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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