UserControl
UserControl

Reputation: 15149

Delegate.BeginInvoke()/EndInvoke() implementation

I want to know how exactly BeginInvoke/EndInvoke methods are implemented on delegates. I know they're automatically generated by the compiler and kinda special so disassemblers cannot handle them. But in the end it's code being executed, right? For some reason i cannot find c# equivalent on the net. Can you help me with that?

Update: OK, i can't have it because it's unmanaged stuff (though i don't understand how it works with ThreadPool which is absolutely managed class). Can you suggest good article that describes the mechanics in details because most of them (like this one) are no use at all.

Upvotes: 4

Views: 1418

Answers (2)

Phil Wright
Phil Wright

Reputation: 22906

These two methods are not generated by the .NET compiler. If you use .NET Reflector or ILDSAM you will not find any MSIL code for the methods. They are actually provided by the CLR itself and so not actually implemented using managed code at all.

At a high level the BeginInvoke uses a thread from the thread pool to execute the delegate. If an exception occurs during the execution then it is caught and remembered. When EndInvoke is called it will rethrow any remembered exception and if not it allows you to get the result from the delegate execution. That is about all it does that is interesting.

Upvotes: 4

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

But in the end it's code being executed, right?

Yes but it is unmanaged code. They are implemented as extern calls into native methods in the CLR. That's why you cannot find C# equivalent.

Upvotes: 3

Related Questions