Asynchronous Method Invocation 【翻译】 (五)

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我需要做些操作来获取委托。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值