C#多线程 - 调用没有控制 [英] C# Multithreading -- Invoke without a Control

查看:134
本文介绍了C#多线程 - 调用没有控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有点熟悉在多线程我已经知道了一点儿,但从来没有使用过它在实践中。

I am only somewhat familiar with multi-threading in that I've read about it but have never used it in practice.

我有一个使用项目第三方库,通过筹款活动分享输入设备的状态。问题是,该库是写这些事件从不同的线程被提出的方式。

I have a project that uses a third party library that shares the status of an input device by raising events. The problem is, the way the library is written these events are raised from a different thread.

我的应用程序并不需要是多线程的,我碰到的很多经典的线程问题(UI控件抱怨与正在互动从不同的线程,集合了被修改为一个代码片段上进行迭代,等等)。

My application does not need to be multi-threaded and I've run into a lot of classic threading issues (UI controls complaining about being interacted with from a different thread, collections that get modified as one piece of code is iterating over it, etc.).

我只是想给予第三方库的事件回到我的UI线程。具体是什么,我认为应该发生的事情是:

I just want the 3rd party library's event to be given back to my UI thread. Specifically what I think should happen is:

我的类接收的事件和处理程序正在被不同的线程比UI运行。我想检测到这种情况(如使用InvokeRequired),然后执行的BeginInvoke相当于把控制权返回到UI线程。然后适当的通知可以发送最多的类层次结构和所有的数据的仅由一个线程触摸

My class receives the event and the handler is being run on a different thread than the UI. I want to detect this condition (like with InvokeRequired), and then perform the equivalent of BeginInvoke to give control back to the UI thread. Then the proper notifications can be sent on up the class hierarchy and all of my data is only touched by the one thread.

的问题是,在接收这些类输入事件是不是由控制产生的,因此不具有InvokeRequired或BeginInvoke的。这样做的原因是,我试图干净独立的UI和基本逻辑。该类仍然在UI线程上运行,它只是没有在类本身内部的任何UI。

The problem is, the class that is receiving these input events is not derived from Control and therefore doesn't have InvokeRequired or BeginInvoke. The reason for this is that I tried to cleanly separate UI and the underlying logic. The class is still being run on the UI thread, it just doesn't have any UI inside the class itself.

现在我被破坏了分离固定的问题。我通过到会从我的课堂上显示的数据和使用的及其调用方法控制的参考。这似乎是它击败因为现在的基础类对我的具体UI类的直接依赖程度将它们分开的整个目的。

Right now I fixed the issue by ruining that separation. I pass in a reference to the control that will be displaying data from my class and using its Invoke methods. That seems like it defeats the whole purpose of separating them because now the underlying class has a direct dependence on my specific UI class.

或许有一种方法可以保存到一个参考跑的构造和螺纹再有什么东西在线程命名空间将执行调用的命令?

Perhaps there's a way to save a reference to the thread that ran the constructor and then there's something in the Threading namespace that will perform the Invoke commands?

有没有办法解决?是我的做法完全错误的?

Is there a way around this? Is my approach completely wrong?

推荐答案

看到的 AsyncOperation 类。创建 AsyncOperation 你想打电话使用的 AsyncOperationManager.CreateOperation 方法。我使用的参数创建通常是空的,但你可以将其设置为任何事情。要调用该线程的方法,使用 AsyncOperation。帖子 方法。

Look into the AsyncOperation class. You create an instance of AsyncOperation on the thread you want to call the handler on using the AsyncOperationManager.CreateOperation method. The argument I use for Create is usually null, but you can set it to anything. To call a method on that thread, use the AsyncOperation.Post method.

这篇关于C#多线程 - 调用没有控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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