简体   繁体   English

c#.Net 4.5线程之间的通信

[英]c# .Net 4.5 Communication between threads

I am building an application where it is possible to monitor some MCU hardware (sensors readings etc) in real time. 我正在构建一个可以实时监视某些MCU硬件(传感器读数等)的应用程序。 For the communication I am using a CAN bus. 为了进行通讯,我使用了CAN总线。

Basically i have 2 threads as of now. 基本上我现在有2个线程。 One is the main thread where the GUI is running and the other is managing/monitoring the communication between the device. 一个是运行GUI的主线程,另一个是管理/监视设备之间的通信。 So the obvious thing is that i need to pass the data from the communications thread to the gui thread. 因此,显而易见的是,我需要将数据从通信线程传递到gui线程。 However what should be the right way to do it? 但是,正确的方法应该是什么? I know how to pass a data back to the calling thread when the child thread has finished working, but in this case the communications thread is running all the time. 我知道在子线程完成工作后如何将数据传递回调用线程,但是在这种情况下,通信线程一直在运行。

Of course the communications logic is represented by a separate class (CANManager). 当然,通信逻辑由单独的类(CANManager)表示。

I have couple of ideas of my own, however I would like to know what is the "right" way how this should be done. 我有一些自己的想法,但是我想知道应该怎么做的“正确”方法。

Thanks in advance :) 提前致谢 :)

Generally in any programming language you need to consider a pub-sub architecture for communicating across threads. 通常,在任何编程语言中,您都需要考虑用于跨线程通信的pub-sub体系结构。 This means that for each thread A which wishes to send a message to thread B, you should post a 'message' or event from that thread onto a queue, to be consumed by another thread when it is free. 这意味着对于每个希望向线程B发送消息的线程A,您都应该将“消息”或事件从该线程发布到队列上,以便在空闲时由另一个线程使用。 If you just google ' Cross Thread communication c# ' you will find numerous articles to read over. 如果您只是谷歌“ Cross Thread communication c# ”,您会发现许多文章需要阅读。

Specifically, in .NET the way to invoke a method or delegate on another (any) thread is to use SynchronizationContext . 具体来说,在.NET中,在另一个(任何)线程上调用方法或委托的方法是使用SynchronizationContext This is common to both Windows Forms and WPF, whereas WPF has a Dispatcher which is distinct to this framework to invoke on the UI thread only. 这对于Windows窗体和WPF都是常见的,而WPF具有Dispatcher,该Dispatcher与该框架不同,只能在UI线程上调用。

There are many frameworks, libraries, patterns available to do this sort of technique. 有许多框架,库和模式可用于执行此类技术。 One of them is the Task Parallel Library . 其中之一是任务并行库 TPL allows you to create a Task, or Task and invoke it on a threadpool, UI, same or specific thread. TPL允许您创建一个Task或Task并在线程池,UI,相同或特定线程上调用它。 TPL allows thread marshalling via the use of Schedulers . TPL允许通过使用Scheduler进行线程编组。 You can use the built-in Schedulers or create your own. 您可以使用内置的调度程序,也可以创建自己的调度程序。 Schedulers use SynchronizationContext at their heart to do the thread marshalling. 调度程序在其心脏处使用SynchronizationContext来进行线程编组。

One particularly interesting pattern of TPL is the ability to run a delegate on one thread and then chain multiple operations on other threads, eg on completion or on error. TPL的一种特别有趣的模式是能够在一个线程上运行委托,然后在其他线程上链接多个操作的能力,例如在完成或出错时。 I would look into the Task Asynchronous Pattern and consider returning Task from async methods so you can chain on them using ContinueWith . 我将研究Task Asynchronous Pattern并考虑从异步方法返回Task,以便可以使用ContinueWith链接到它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM