✅ 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
How would that work? Events are called on the caller's thread
Not sure what you mean? Events are threadsafe as far as I know?
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?
So you're saying if an event is triggered on one thread, subscribers on other threads won't be called?
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 onI'm very sorry but I don't understand what you're saying.
canton7#1569
REPL Result: Success
Console Output
Compile: 852.274ms | Execution: 108.682ms | React with ❌ to remove this embed.
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?
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
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.