C
C#17mo ago
dreamdoctor

✅ Suggestions for something like ConcurrentQueue with events

C# beginner question... I have an app with multiple threads. I need to pass data from one thread to the other using some form of FIFO queue. I can use ConcurrentQueue, however it doesn't appear to have any events available for enqueue or dequeue, which means I would need to poll the queue for new items, which doesn't seem like the right thing to do. I could add my own events, but that also seems like reinventing the wheel. Surely this is a very common situation. Are there any threadsafe queue techniques or libraries I can use with events available?
11 Replies
canton7
canton717mo ago
How would that work? Events are called on the caller's thread
dreamdoctor
dreamdoctor17mo ago
Not sure what you mean? Events are threadsafe as far as I know?
canton7
canton717mo ago
It's thread-safe to subscribe and unsubscribe, sure But when you invoke an event, all subscribers get called synchronously, on that thread Which probably isn't what you want if you want to tell a different thread that there's a new item available?
dreamdoctor
dreamdoctor17mo ago
So you're saying if an event is triggered on one thread, subscribers on other threads won't be called?
canton7
canton717mo ago
Not quite. I'm saying that there's no such thing as subscribing "on a thread" When you raise an event, all subscribers are called synchronously, on your thread. Regardless of what thread the eventName += Handler subscription was executed on
dreamdoctor
dreamdoctor17mo ago
I'm very sorry but I don't understand what you're saying.
MODiX
MODiX17mo ago
canton7#1569
REPL Result: Success
using System;
using System.Threading;

var otherThread = new Thread(_ =>
{
Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} subscribing to event");
EventSource.SomeEvent += (o, e) => Console.WriteLine($"Event raised on thread {Thread.CurrentThread.ManagedThreadId}");
});
otherThread.Start();

Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} raising event");
EventSource.Raise();

public static class EventSource
{
public static event EventHandler? SomeEvent;
public static void Raise() => SomeEvent?.Invoke(null, EventArgs.Empty);
}
using System;
using System.Threading;

var otherThread = new Thread(_ =>
{
Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} subscribing to event");
EventSource.SomeEvent += (o, e) => Console.WriteLine($"Event raised on thread {Thread.CurrentThread.ManagedThreadId}");
});
otherThread.Start();

Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} raising event");
EventSource.Raise();

public static class EventSource
{
public static event EventHandler? SomeEvent;
public static void Raise() => SomeEvent?.Invoke(null, EventArgs.Empty);
}
Console Output
Thread 5 subscribing to event
Thread 11 raising event
Event raised on thread 11
Thread 5 subscribing to event
Thread 11 raising event
Event raised on thread 11
Compile: 852.274ms | Execution: 108.682ms | React with ❌ to remove this embed.
dreamdoctor
dreamdoctor17mo ago
Ok, so you're saying that if an event is defined in thread #1, and a method is attached to that event in thread #2, it will still execute in thread #1 when that event is triggered?
canton7
canton717mo ago
It's nothing to do with what thread the event is "defined" in (whatever that means), or what thread an event is subscribed to in It's only to do with what thread invokes the event Threads run code. Code isn't "attached" to a thread in any way. Just because you used one thread to run a bit of code one time, doesn't mean that bit of code is automatically run on that thread in the future
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts