Sending messages between threads in C#

17,038

Solution 1

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.

Solution 2

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://docs.microsoft.com/en-us/dotnet/api/system.threading.channels https://devblogs.microsoft.com/dotnet/an-introduction-to-system-threading-channels/

Share:
17,038
Ankata
Author by

Ankata

Updated on June 17, 2022

Comments

  • Ankata
    Ankata almost 2 years

    How can I send and receive messages between threads?

    • SubniC
      SubniC over 13 years
      Hi, take a look of this tutorial it may help :) dreamincode.net/forums/topic/…
    • Brian Rasmussen
      Brian Rasmussen over 13 years
      Do you want to coordinate work or share data between threads?
    • wj32
      wj32 over 13 years
      What exactly are you trying to do? "Sending messages" across threads is a concept that is not really that useful in most situations.
  • Donal Fellows
    Donal Fellows over 13 years
    That is only for one thread to wait for the termination of another. There are many other inter-thread communication patterns and I'd be utterly shocked if C# didn't support other messaging patterns (even though I don't know enough to answer the Q).