In C#, what thread will Events be handled in?
Posted
by Ben
on Stack Overflow
See other posts from Stack Overflow
or by Ben
Published on 2010-03-17T02:53:15Z
Indexed on
2010/03/17
3:01 UTC
Read the original article
Hit count: 420
Hi, I have attempted to implement a producer/consumer pattern in c#. I have a consumer thread that monitors a shared queue, and a producer thread that places items onto the shared queue. The producer thread is subscribed to receive data...that is, it has an event handler, and just sits around and waits for an OnData event to fire (the data is being sent from a 3rd party api). When it gets the data, it sticks it on the queue so the consumer can handle it.
When the OnData event does fire in the producer, I had expected it to be handled by my producer thread. But that doesn't seem to be what is happening. The OnData event seems as if it's being handled on a new thread instead! Is this how .net always works...events are handled on their own thread? Can I control what thread will handle events when they're raised? What if hundreds of events are raised near-simultaneously...would each have its own thread?
Thank in advance! Ben
© Stack Overflow or respective owner