像[代表]在哪里CLR定义的方法.BeginInvoke记录? [英] Where are CLR-defined methods like [delegate].BeginInvoke documented?

查看:283
本文介绍了像[代表]在哪里CLR定义的方法.BeginInvoke记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像我的问题确实不好措辞,并获得过不好。所以我希望这个完整的改写帮助...

Seems like my question was poorly worded indeed, and poorly received too. So I hope that this complete rephrasing helps...

MSDN告诉明确规定:
<一href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx\">Control.BeginInvoke()执行该控件的句柄在创建线程上委托,通常这会是GUI线程。和<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.begininvoke.aspx\">Dispatcher.BeginInvoke()将在其中创建调度对象的线程上运行。这将是我所创建的任何线程。

MSDN tells clearly specifies: Control.BeginInvoke() Executes a delegate on the thread that the control's handle was created on, normally this would be the GUI thread. And Dispatcher.BeginInvoke() will run on the thread where the Dispatcher object was created. This would be any thread created by me.

但对于代表们的 CLR自动定义的BeginInvoke和EndInvoke的和这些调用在线程池线程上运行,而不是。除了这个令人惊讶的略有不同的行为,我不知道我怎么能找到,可以自动得到实现的所有功能规格。

But for delegates "the CLR automatically defines BeginInvoke and EndInvoke" and these calls run on a ThreadPool-thread instead. Apart from this slightly surprising different behaviour I wonder how I can find the specs of all functions that automatically get implemented.

例如:智能影音感表明,我委托具有DynamicInvoke()。 {} 确实有DynamicInvoke(),这可能意味着我代表它继承。但是代表{}没有的BeginInvoke()。和委托{}有我的代表还没有几个功能。同时我代表得到了GetObjectData使用()方法。这似乎是来自ISerializable的。

For example: Intelli-sense shows that my delegate has a DynamicInvoke(). Class System.Delegate{} does have a DynamicInvoke() which might imply that my delegate inherits it. But Delegate{} has no BeginInvoke(). And Delegate{} has several functions that my delegate has not. Also my delegate gets a GetObjectData() method. And this seems to come from ISerializable.

所以在最后,委托出现得到它的方法,从(1)CLR自动,(2)代表的一些子集{}可能MulticastDelegate {},并可能(3)ISerializble。 我在哪里可以找到的所有方法委托获得一个COM prehensive规范?特别有趣的是的BeginInvoke(),它的确切签名,与该名称上述两种方法有不同的集签名。

So in conclusion, a delegate appears gets its methods from (1) the CLR "automatically", (2) some subset of Delegate{} possibly MulticastDelegate{}, and possibly (3) ISerializble. Where can I find a comprehensive specification of all methods a delegate gets? Especially interesting is BeginInvoke(), and it's exact signature, as the two aforementioned methods with that name have different sets of signatures.

[有人在编辑建议,代表是代表。我敢说,这不是。]

[Someone suggested in an edit that a "delegate" is a "Delegate". I daresay, it is not.]

感谢

推荐答案

在Control.Begin /结束/ Invoke的()和Dispatcher.Begin /结束/ Invoke的()方法具有相同的名称和有点类似行为的委托的开始/完/ Invoke的()方法,但它肯定是最好打消这个想法,他们是相同的。最重要的区别是,委托的方法的类型安全的,东西是完全从控制与调度员版本失踪。运行时行为是非常不同的。

The Control.Begin/End/Invoke() and Dispatcher.Begin/End/Invoke() methods have identical names and somewhat similar behavior to a delegate's Begin/End/Invoke() methods but it is certainly best to scrap the idea that they are the same. The most important difference is that a delegate's methods are type-safe, something that's completely missing from the Control and Dispatcher versions. Runtime behavior is very different as well.

这是管理委托的规则在CLI规范规定了详细, ECMA 335 ,章II.14.6。最好是阅读这一章,我只给一个大纲。

The rules that govern a delegate are spelled out in detail in the CLI spec, ECMA 335, chapter II.14.6. It is best to read the chapter, I'll just give a synopsis.

一个委托声明转化为从MulticastDelegate继承(如CLI规范规定不代表)的类。这个类总是正好有4名成员,其运行时实现是由CLR提供的:

A delegate declaration is transformed into a class that inherits from MulticastDelegate (not Delegate as specified in the CLI spec). That class always has exactly 4 members, their runtime implementation is provided by the CLR:


  • 一个构造函数的对象,一个IntPtr。对象是Delegate.Target,所述IntPtr的是目标的方法,Delegate.Method的地址。这些成员使用后,当你调用委托,目标物业供应的如该委托绑定的方法本的参考是一个实例方法,空的静态方法。该方法属性决定哪种方法被调用。你不直接指定这些参数,当你使用new运算符或+ =运算符订阅事件处理程序的编译器提供他们。随着大量在事件的情况下,语法糖,你不必使用的新的的运营商明确。

  • a constructor that takes an object and an IntPtr. The object is the Delegate.Target, the IntPtr is the address of the target method, Delegate.Method. These members are used later when you invoke the delegate, the Target property supplies the this reference if the method to which the delegate is bound is an instance method, null for a static method. The Method property determines which method gets invoked. You don't specify these arguments directly, the compiler supplies them when you use the new operator or subscribe an event handler with the += operator. With lots of syntax sugar in the case of events, you don't have to use the new operator explicitly.

这是invoke()方法。该方法的参数动态生成并匹配委托声明。调用invoke()方法运行在同一个线程,一个的同步的呼叫委托目标的方法。你很少使用它在C#中,你只需要使用语法糖,允许委托对象由只使用对象名称来调用,后面括号。

an Invoke() method. The arguments of the method are dynamically generated and match the delegate declaration. Calling the Invoke() method runs the delegate target method on the same thread, a synchronous call. You rarely use it in C#, you just use the syntax sugar that allows a delegate object to be invoked by just using the object name, followed by parentheses.

一的BeginInvoke()方法,提供了一种方法,使一个的异步的电话。该方法很快完成,而目标方法正在执行,类似于ThreadPool.QueueUserWorkItem但类型安全的参数。返回类型总是System.IAsyncResult,用来找出当异步调用完成并提供给EndInvoke会()方法。第一个参数是一个可选的System.AsyncCallback委托对象,异步调用完成后,它的目标将自动被调用。第二个参数是的,它会被原样传递到回调的,有用的跟踪状态的一个可选的对象。额外的参数动态生成并匹配委托声明。

a BeginInvoke() method, provides a way to make an asynchronous call. The method quickly completes while the target method is busy executing, similar to ThreadPool.QueueUserWorkItem but with type-safe arguments. The return type is always System.IAsyncResult, used to find out when the asynchronous call is completed and supplied to the EndInvoke() method. First argument is an optional System.AsyncCallback delegate object, it's target will automatically be called when the asynchronous call is complete. Second argument is an optional object, it will be passed as-is to the callback, useful to keep track of state. Additional arguments are dynamically generated and match the delegate declaration.

这是EndInvoke会()方法。这需要IAsyncResult类型的一个参数,你必须通过你的BeginInvoke得到一个()。它完成了异步调用并释放资源。

an EndInvoke() method. It takes a single argument of type IAsyncResult, you must pass the one you got from BeginInvoke(). It completes the asynchronous call and releases resources.

您委托对象上看到的任何附加的方法是从基类,MulticastDelegate和代表继承而来的。像DynamicInvoke()和GetObjectData使用()。

Any additional methods you see on a delegate object are the ones that are inherited from the base classes, MulticastDelegate and Delegate. Like DynamicInvoke() and GetObjectData().

异步调用是棘手的那些和你很少需要使用它们。他们实际上是在Silverlight中不可用。委托目标方法上的任意线程池中的线程中运行。任何未处理的异常可能抛出被捕获并终止线程,但不是你的程序。您的必须的调用EndInvoke(),不这样做会造成资源泄漏10分钟。 ()如果目标方法抛出异常,那么它将被当你调用EndInvoke重新提出。您可以在线程池中的线程没有控制,没有办法取消或终止它。该任务或线程类是更好的选择。

Asynchronous calls are the tricky ones and you rarely need to use them. They are in fact not available in Silverlight. The delegate target method runs on an arbitrary thread-pool thread. Any unhandled exception it might throw is captured and terminates the thread but not your program. You must call EndInvoke(), not doing so will cause a resource leak for 10 minutes. If the target method threw an exception then it will be re-raised when you call EndInvoke(). You have no control over the thread-pool thread, there is no way to cancel or abort it. The Task or Thread classes are better alternatives.

MSDN是相关的,委托类型的方法的的记录。它假定你知道他们做什么,他们看起来就像从规范和委托声明什么。

MSDN is relevant, the methods of a delegate type are not documented. It assumes you know what they do and what they look like from the specification and the delegate declaration.

这篇关于像[代表]在哪里CLR定义的方法.BeginInvoke记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆