Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 299
Oscar Mederos Avatar asked May 09 '11 07:05

Oscar Mederos


People also ask

What is firing of an event?

Events can be fired by user actions: for example a user clicks on a chart, or can be internal: for example, firing an event every 10 seconds. You can register a Javascript method to be called whenever certain events are fired, possibly with data specific to that event.

What is event handling in C?

C event handlers allow you to interface more easily to external systems, but allowing you to provide the glue logic. The POS includes a small open source C compiler (TCC) which will dynamically compile and link your C code at runtime. Alternatively, you can precompile and ship a DLL/EXE with your functions.

What is an event in C sharp?

Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.

What is event in C# stackoverflow?

The event is an instance of a delegate. Since an event is an instance of a delegate, then we have to first define the delegate. Assign the method / methods to be executed when the event is fired (Calling the delegate) Fire the event (Call the delegate)


1 Answers

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.

like image 190
Stilgar Avatar answered Sep 21 '22 05:09

Stilgar