必须在每次的BeginInvoke跟着一个EndInvoke会? [英] Must every BeginInvoke be followed by an EndInvoke?

查看:265
本文介绍了必须在每次的BeginInvoke跟着一个EndInvoke会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该页面在MS文档,在Windows覆盖异步窗体应用程序,规定:

  

您可以调用EndInvoke从委托,如果neccesary检索返回值,但是的这不是必需的。的(强调)

本页面覆盖了异步委托的一般情况下,规定不同的东西:

  

不管你使用哪种技术,总是调用EndInvoke来完成你的异步调用。

这两个似乎是在直接冲突。

这是真的吗?有人能解释一下吗?

又见,后由菲尔哈克

  

相关阅读:<一href="http://stackoverflow.com/questions/532722/is-endinvoke-optional-sort-of-optional-or-definitely-not-optional">Is EndInvoke会选,排序可选,绝对不是可有​​可无的?

解决方案

除非接口的文件明确表示,否则你必须调用EndInvoke为每次调用BeginInvoke的地方。的主要原因是的EndInvoke是其中的所有者可以安全地自由某些资源可被分配给的BeginInvoke呼叫(如一个的WaitHandle)的唯一时间。

但也有例外情况。 API,例如Control.BeginInvoke不需要EndInvoke会,但它是明确的文件中。

This page in the MS documentation, covering asynchrony in Windows Forms applications, states:

You can call EndInvoke to retrieve the return value from the delegate, if neccesary, but this is not required. (emphasis added)

This page covering the general case of asynchronous delegates, states something different:

No matter which technique you use, always call EndInvoke to complete your asynchronous call.

These two seem to be in direct conflict.

Which is true? Can someone explain?

see also, a post by Phil Haack.

Related: Is EndInvoke optional, sort-of optional, definitely not optional?

解决方案

Unless the documentation for an interface explicitly says otherwise you must call EndInvoke for every place you call BeginInvoke. The primary reason is that EndInvoke is the only time where the owner can safely free certain resources that may be allocated for the BeginInvoke call (such as a WaitHandle).

But there are exceptions to this rule. APIs such as Control.BeginInvoke do not require an EndInvoke but it's explicit in the documentation.

这篇关于必须在每次的BeginInvoke跟着一个EndInvoke会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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