In .NET, what thread will Events be handled in?

46,273

Solution 1

After re-reading the question, I think I understand the problem now. You've basically got something like this:

class Producer
{
    public Producer(ExternalSource src)
    {
        src.OnData += externalSource_OnData;
    }

    private void externalSource_OnData(object sender, ExternalSourceDataEventArgs e)
    {
        // put e.Data onto the queue
    }
}

And then you've got a consumer thread that pulls stuff off that queue. The problem is that the OnData event is fired by your ExternalSource object - on whatever thread it happens to be running on.

C# events are basically just an easy-to-use collection of delegates and "firing" an event just causes the runtime to loop through all of the delegates and fire them one at a time.

So your OnData event handler is getting called on whatever thread the ExternalSource is running on.

Solution 2

Unless you do the marshaling yourself, an event will execute on whatever thread is invoking it; there's nothing special about the way events are invoked, and your producer thread doesn't have an event handler, your producer thread simply said "hey, when you fire this event, call this function". There's nothing in there that causes the event execution to occur on the attaching thread, nor on its own thread (unless you were to use BeginInvoke rather than invoking the event's delegate normally, but this will just execute it on the ThreadPool).

Solution 3

Raising an event with Invoke is the same as calling a method - it gets executed in the same thread you raised it.

Raising an event with BeginInvoke uses ThreadPool. Here are some minor details

Share:
46,273
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

  • Admin
    Admin about 14 years
    Thanks Codeka,you've got it exactly. I'll check into what thread the externalSource events are occuring. I appreciate the help.
  • Satyajit Mohanty
    Satyajit Mohanty almost 7 years
    things are different when events are fired using Invoke and BeginInvoke..BeginInvoke invokes the event subscriber in a different thread from threadpool(in a background thread)
  • Robert F.
    Robert F. about 6 years
    @SatyajitMohanty You mention Invoke too. Is Invoke executed on a new thread?