Asynchronous Method Invocation 【翻译】 (五)

来源:互联网 发布:淘宝查虚假交易时间 编辑:程序博客网 时间:2024/05/21 07:47

Using the Callback delegate, Hollywood style "Don�t call me I will call you!"

At this point, you should understand how parameters can be passed, how to pass state, and understand the fact that your method is executed on a thread within the ThreadPool. The only thing I didn�t really cover is the idea of being notified when the method is finished executing. After all, blocking and waiting for the method to finish does not accomplish much. In order to be notified when a method is complete, you must supply a callback delegate on the BeginInvoke. OK, example! Look at the following two functions:

 

使用委托回调,就像好莱坞的风格"Don't call me I will call you!"

我们应该理解了参数怎么传递,状态怎么传递,理解了方法如何在线程池里的执行。唯一没有提到的是方法执行完毕时,我们怎么被通知到。

毕竟,阻塞和等待方法的完成,并不那么好搞。当一个方法完成,我们要被通知到的话,你必须在BeginInvoke上提供一个回调委托。好吧,例子说明!看下下面的2个函数:

 

 

In here, you can see that we passed a delegate to the function CallBack when calling BeginInvoke. .NET will call us when the method FooWithOutAndRefParameters completes execution. As before, we all know that we must call EndInvoke if we want to get our output parameters. Notice that in order to call EndInvoke, I needed to do some gymnastics to get the delegate.

 

在这里,我们可以了解到,当调用BeginInvoke时候,我们传递一个委托回调CallBack函数。当FooWithOutAndRefParameters完全执行完毕,.NET将会通知我们。在这之前,如果我们想获得输出参数,我们必须调用EndInvoke。注意到,想要调用EndInvoke我需要做些操作来获取委托。

 

 

原创粉丝点击