Asynchronous Method Invocation 【翻译】 (四)

来源:互联网 发布:如何制作淘宝店铺招牌 编辑:程序博客网 时间:2024/05/17 22:38

What they don�t want you to know about IAsyncResult

You should be wondering how EndInvoke is able to give us the output parameters and the updated ref parameters. Or, better yet, how EndInvoke is able to throw that exception we threw in our function. For example, say we called BegineInvoke on Foo, and then Foo finished executing, and now, we normally would call EndInvoke, but what if we decide to call EndInvoke 20 minutes after Foo is finished? Notice that EndInvoke will still give you those output or ref parameters, and it would still throw that exception (if one was thrown). So, where is all that information stored? How come EndInvoke is able to get all that data long after the function is completed? Well� the key is with the IAsyncResult object! I decided to explore this object a little more, and as I suspected, it is this object that keeps all the information regarding your function call. Notice that EndInvoke takes one parameter, it is an object of type IAsyncResult. This object contains information such as:

  • Is the function completed?
  • A reference to the delegate used for BeginInvoke
  • All output parameters and their values
  • All ref parameters and their updated values
  • Return value
  • An Exception if one was thrown
  • And more�

他们不想让你知道的 IAsyncResult

你应该知道了EndInvoke能给我们提供参数输出,也能更新ref参数。在函数中EndInvoke能抛出异常。例如,我们用BeginInvoke调用函数Foo,接着Foo函数指向完毕,然后,通常我们会调用EndInvoke。但是,如果我们需要在Foo完成后20分钟再要调用EndInvoke该怎么办那?EndInvoke仍然会做相应的output和ref参数的操作,仍然会抛出异常(如果异常存在的话)。所有这些信息存在哪里那?函数已经完成后那么长时间,EndInvoke是怎么把数据保存的?好吧,答案就是IAsyncResult对象! 我觉得更多研究下这个对象,这个对象保留了所有的IAsyncResult相关的东东哦。这个对象包含了信息如下:

 

* 函数是否完成?

* 为BeginInvoke提供的委托

* 所有输出参数和他们的值

* 所有的ref参数和他们更新后的值

* 返回值

* 如果异常存在,抛出异常

* 其他

 

IAsyncResult may seem very innocent, because it is just an interface to a few little properties, but in fact, it is an object of type System.Runtime.Remoting.Messaging.AsyncResult.

 

IAsyncResult视乎很傻很天真(呵呵),因为他只有一个接口和一些属性,但实际上,它是一个对象,类型是System.Runtime.Remoting.Messaging.AsyncResult的高级东东哦。

 

 

 

Now, if we dig a little deeper, we will find that AsyncResult contains an object called _replyMsg of type System.Runtime.Remoting.Messaging.ReturnMessage, and what do you know� the holy grail has been found!

 

现在,如果我们深入研究,我们发现AsyncResult包含一个System.Runtime.Remoting.Messaging.ReturnMessage类型的叫什么_replyMsg的东东。找到的都是好东东哦。。

 

 

I had to shrink the above image so you won�t need to scroll to the right to read it, you can simply click on the image to view it。

上面图片有点小,不行就点开查看。

 

We can clearly see our return value, our output parameter, and our ref parameters. There is even an exception property to hold the exception. Notice that I expanded, in the debug window for OutArgs to show, the value 200 and a reference to the newly allocated ArrayList. You can also see in the property ReturnValue, the string �Thank you for reading this article�. If we had an exception, then the EndInvoke would throw it for us to catch it. I think this is proof enough to conclude that all the information regarding your function call is saved with that little IAsyncResult object you get back from BeginInvoke, it is like a key to your data. If we lose this object, we will never know our output parameters, ref parameters, and return value. It will also not be possible to catch an exception without this object. It�s the key! You lose it, and the info is lost forever in the maze of the .NET runtime� OK, getting a little carried away here. I think I made my point.

 

我们可以很清楚的看到返回值,输出参数,ref参数。甚至捕获异常是的异常属性。在展开栏里,debug窗口可以看到OutArgs,值是200,

还有指向最新分配的ArrayList。你也可以看到ReturnValue属性,字符"Thank you for reading this article".如果有异常的话,在EndInvoke会捕获它而且抛出它。我想,这些东东能足够让我们推断出函数所有的信息都存放在IAsyncResult 对象里,你能从BeginInvoke获得。这是数据的关键要素。如果没有这些东东,你就永远不能知道输出参数,ref参数,返回值。永远不能捕获异常。这个是很关键很重要的东东哦。你要是弄丢了这样东东,你就永远对.NET这些玩意迷迷糊糊了。好吧,这些玩意说的差不多了,我已经把要点都讲到了。