简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM