Oscar Mederos
Oscar Mederos

Reputation: 29863

Do firing events in C# block the current thread execution?

If I'm firing the event:

var handler = OnMyEvent;

if (handler != null)
{
    handler(some_info);
}

then will the execution thread wait until all suscriber methods return to continue the execution after line:

handler(some_info);

?

Or events are fired "in another thread", meaning that it automatically goes to the next line after handler(some_info)?

Upvotes: 5

Views: 2388

Answers (2)

Stilgar
Stilgar

Reputation: 23571

Events are fired on the same thread and it will block until they are completed. Of course the event handling code itself can spawn another thread and return immediately but this is completely different matter.

Also note that events like button clicks in a desktop applications like Windows Forms apps are put on a message queue and will fire one at a time. i.e. if you press a button and then press another button the second button event will not fire until the first is completed. Also the form will not repaint and will be "not responding" because painting the form is also an event.

Upvotes: 10

Noel Kennedy
Noel Kennedy

Reputation: 12268

Events are fired in the thread that raised them.

Upvotes: 2

Related Questions