开发者

Why AsyncCallback?

I have read that the AsyncCallback function will开发者_如何转开发 do all the post processing tasks after a delegate call completes. My question is what is the difference of writing the post processing tasks after/below EndInvoke, as EndInvoke will wait until the delegate call returns.


The point of using a delegate's BeginInvoke() method is that the target will run asynchronously. If you call EndInvoke() right after BeginInvoke() then there is no longer any point in using it. You'll get the exact same behavior by simply calling the delegate directly, minus the overhead and the threading headaches.

The value is getting the callback when the target method completes, asynchronously right after it does. Only then call EndInvoke(), it completes immediately. That cleans up the system resources and re-throws an exception when the target method threw one. Do not skip calling EndInvoke() as suggested in the upvoted answer, you'll leak the resources for at least 10 minutes.


You could choose not to invoke EndInvoke at all and only rely on the callback. This way the initiation comes from the delegate itself and not from you, and you dont have to decide when the invocation will complete.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜