Delegate implementation .BeginInvoke () / EndInvoke ()

I want to know how exactly the BeginInvoke/EndInvoke methods are implemented for delegates. I know that they are automatically generated by the compiler and seemingly on purpose, therefore, disassemblers cannot process them. But in the end, this code executes, right? For some reason, I cannot find the C # equivalent on the net. Can you help me?

Update: OK, I canโ€™t use it because it is unmanaged material (although I donโ€™t understand how it works with ThreadPool , which is an absolutely managed class). Can you suggest a good article that details mechanics because most of them (like this one ) are not used at all.

+4
source share
2 answers

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

At a high level, BeginInvoke uses a thread from a thread pool to execute a delegate. If an exception occurs during execution, it is captured and remembered. When EndInvoke is called, it will restore any remembered exception, and if it does not allow to get the result from the delegate. This applies to everything that is interesting.

+4
source

But in the end it runs, right?

Yes, but this is unmanaged code. They are implemented as extern calls for native methods in the common language runtime. That is why you cannot find the C # equivalent.

+3
source

All Articles