C# 中线程之间发送消息

发布于 2024-10-03 23:22:30 字数 22 浏览 0 评论 0 原文

如何在线程之间发送和接收消息?

How can I send and receive messages between threads?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黯然 2024-10-10 23:22:30

例如,一种解决方案是共享并发队列(尽管其名称)ConcurrentQueue< /a>.这将允许您将一个对象从一个线程入队,并让另一个线程(或其他线程)从队列中出队。由于它是通用解决方案,您可以传递强类型项,从 stringAction 的任何内容都可以,当然也可以传递您自己的自定义消息类。

这种方法只有一个限制,ConcurrentQueue 类仅从 .NET 4.0 开始可用。如果您需要针对以前版本的 .NET,您需要寻找第三方库。例如,您可以采用 来自 mono 的 ConcurrentQueue 来源

这些队列工作的一般方法是通过一个链表,并使用旋转来实现乐观并发控制以实现同步。据我所知,这是可变大小并发队列的最新技术。现在,如果您事先知道消息负载,则可以尝试固定大小的方法或有利于入队和出队而不是增长的解决方案(这将是基于数组的队列)。


完整披露(根据 faq):我是这些第三方库之一的作者...... 我的库(nuget可用),它包括一个针对旧版本.NET的向后移植ConcurrentQueue,基于关于自定义实现。你可以在Theraot.Collections.ThreadSafe.SafeQueue下找到底层结构,它是一个数组链表(保存在对象池中),通过这样做,我们不需要复制数组以增长(因为我们只是将另一个节点添加到列表中),并且我们不需要经常依赖同步机制(因为添加或删除项目不会经常修改列表)。

注意:这个问题曾经链接到 HashBucket,它托管在另一个存储库上,是我解决该问题的旧解决方案。该项目已停止,请使用我上面提到的版本。

One solution would be share a concurrent queue, for example (albeit its name) ConcurrentQueue. This will allow you to enqueue an object from one thread and have the other thread (or others threads) dequeue from the queue. As it is a generic solution, you may pass strongly typed items, anything from string to Action will do, or your own custom message class of course.

Threre is just one limitation with this approach, the class ConcurrentQueue is only available from .NET 4.0 onwards. If you need this for a previous version of .NET you need to look for a third party libary. For example you can take the source for ConcurrentQueue from mono.

The general approach over which those queues work is by having a linked list and they resource to optimistic concurrency control using spinning for synchronization. As far as I know, this is the state of art for concurrent queues of variable size. Now, if you know the message load beforehand you can try a fixed size approach or a solution that favors enqueue and dequeue over growing (that would be an array based queue).


Full disclouser (according to faq): I'm the author of one of those third party libraries... my libraries (nuget available), it includes a backport ConcurrentQueue for old versions of .NET, based on a custom implementation. You can find the underlaying structure under Theraot.Collections.ThreadSafe.SafeQueue, it is a linked list of arrays (which are kept in an object pool), by doing it this way, we do not need to copy the arrays to grow (because we just add another node to the list), and we do not need to rely on synchronization mechanisms as often (because adding or removing an item does not modify the list often).

Note: this question used to link to HashBucket, which is hosted on another repository, and was my old solution for the problem. That project is discontinued, please use the version I mention above.

我不吻晚风 2024-10-10 23:22:30

这是一个老问题,但仍然是一个相关的主题......

生产者/消费者方法可以用作此类问题的可能解决方案。 .NET Core 从 3.0 版本开始,有一个命名空间,其中包含可以以简单方式处理该问题的工具。

看一下 System.Threading.Channels:

https:// learn.microsoft.com/en-us/dotnet/api/system.threading.channels
https://devblogs.microsoft.com/dotnet/an -系统线程通道简介/

This is an old question, but still a relevant topic...

A producer/consumer approach may be used as possible solution for a problem like this. .NET Core, from version 3.0, has a namespace with tools to deal with that in a simple way.

Take a look at System.Threading.Channels:

https://learn.microsoft.com/en-us/dotnet/api/system.threading.channels
https://devblogs.microsoft.com/dotnet/an-introduction-to-system-threading-channels/

尛丟丟 2024-10-10 23:22:30

我认为您正在谈论 在线程之间加入?这个。

I think you are talking about Joining between threads? and this.

去了角落 2024-10-10 23:22:30

一种方法是创建一个类,其中包含您将为线程调用的方法。
该类不仅可以有方法,还可以有更多的内容。它可以具有父线程可以访问的成员。
鉴于此,父级可以读取和写入这些成员,这样在线程的整个生命周期中两个线程之间就有一种通信方式。

One way to do it is to create a class that has the method that you will call for the thread.
That class can have more than just the method; it can have members that the parent thread can have access to.
Given that, the parent can read from and write to those members, that way there is a means of communication between the two threads throughout the span of the thread's life.

落花浅忆 2024-10-10 23:22:30

您可以在 .Net 中使用许多线程同步原语,例如 EventWaitHandle、Mutex、Semaphores 等。这里有 MSDN 上的一个有用链接,可以了解如何使用。 - https://learn.microsoft.com /en-us/dotnet/standard/threading/overview-of-synchronization-primitives

There are many thread synchronization primitives you can use in .Net such as EventWaitHandle, Mutex, Semaphores etc. Here is a useful link on MSDN to find out how. - https://learn.microsoft.com/en-us/dotnet/standard/threading/overview-of-synchronization-primitives

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文