Regenschein
Regenschein

Reputation: 1502

What does the .NET Virtual Machine do when I raise an event?

What exactly is /technically/ happening when I raise an event in .NET? I couldn't answer this question myself via Google.

I don't mean: the event is raised and suddenly some handler is being called, I mean what is happning in between? Are the events somehow stored on an invisible event stack? Are they queued somewhere? How many events can I raise? etc.

I hope someone can shed some light on how the .NET virtual machine processes events.

Upvotes: 2

Views: 71

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064114

Regular .NET events are usually simply delegate invocations, which are not very much different to regular method invocations. The only subtle difference is that because delegates are multi-cast, it has to unroll the full invocation list of all subscribers, and invoke them in turn. This all happens during the delegate .Invoke(). So no, there is no special event stack / queue.

However! Some things on UI technologies (winforms, WPF, etc) do use the Windows message loop - so in those cases, yes, there is a logical queue.

Each delegate subscription (i.e. foo.SomeEvent += this.SomeHandler) involves two things: a target instance (the this) and a target method (SomeHandler). When invoked, the delegate simply invokes the method against the specified target. Slightly magic, but not that different to regular method invoke.

Upvotes: 4

Related Questions