简体   繁体   English

Delegate.BeginInvoke()/ EndInvoke()实现

[英]Delegate.BeginInvoke()/EndInvoke() implementation

I want to know how exactly BeginInvoke/EndInvoke methods are implemented on delegates. 我想知道如何在委托上实现BeginInvoke/EndInvoke方法。 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. 由于某种原因,我无法在网上找到c#等价物。 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). 更新:好的,我不能拥有它,因为它是非托管的东西(虽然我不明白它如何与ThreadPool工作,这是绝对托管的类)。 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. 这两种方法不是由.NET编译器生成的。 If you use .NET Reflector or ILDSAM you will not find any MSIL code for the methods. 如果使用.NET Reflector或ILDSAM,则无法找到方法的任何MSIL代码。 They are actually provided by the CLR itself and so not actually implemented using managed code at all. 它们实际上是由CLR本身提供的,因此根本不使用托管代码实现。

At a high level the BeginInvoke uses a thread from the thread pool to execute the delegate. 在较高级别,BeginInvoke使用线程池中的线程来执行委托。 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. 当调用EndInvoke时,它将重新抛出任何记住的异常,如果不是,它允许您从委托执行中获取结果。 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. 它们被实现为对CLR中的本机方法的extern调用。 That's why you cannot find C# equivalent. 这就是为什么你找不到C#的等价物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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