每个 BeginInvoke 后面都必须跟一个 EndInvoke 吗?

发布于 2024-08-02 15:13:04 字数 833 浏览 4 评论 0原文

MS 文档中的此页面,涵盖 Windows 窗体应用程序中的异步,状态:

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

此页面涵盖了异步委托的一般情况,说明了一些不同的内容:

无论您使用哪种技术,始终调用 EndInvoke 来完成异步调用。

这两者似乎存在直接冲突。

哪个是真的?有人可以解释一下吗?

另请参阅 Phil Haack 的帖子

相关: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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追星践月 2024-08-09 15:13:04

除非接口文档明确说明,否则您必须为调用 BeginInvoke 的每个位置调用 EndInvoke。主要原因是 EndInvoke 是所有者可以安全释放可能为 BeginInvoke 调用分配的某些资源(例如 WaitHandle)的唯一时间。

但这条规则也有例外。 Control.BeginInvoke 等 API 不需要 EndInvoke,但文档中对此有明确说明。

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.

遗弃M 2024-08-09 15:13:04

两者都是正确的——它们是不同的称呼。

一般,您应该始终调用 EndInvoke 以确保释放异步调用获取的任何资源。

但是,Windows 窗体团队保证您不需要对 Control.Invoke 执行此操作。不过,您可能很需要为 ISynchronizeInvoke 的其他实现执行此操作。

Both are true - they're different calls.

In general you should always call EndInvoke to ensure that any resources acquired by the asynchronous call are released.

However, the Windows Forms team has guaranteed that you don't need to do this for Control.Invoke. You may well need to do it for other implementations of ISynchronizeInvoke though.

扮仙女 2024-08-09 15:13:04

我之前曾对委托使用过即发即忘方法,其结果是“如果可用的话很有用,但不是必需的”。请记住,该方法没有完成保证。特别是,这里是我使用它的一个地方:

  • 启动委托来检查应用程序更新
  • 委托开始超时的 Web 请求
  • 如果发生错误/超时,或者应用程序是最新的,则该方法仅返回
  • If应用程序已过时,我放置了一条非焦点窃取系统托盘消息,说明这一点(除非有更新可用,否则没有系统托盘图标)

无论哪种方式,应用程序都会不间断地继续运行。

I've used the fire-and-forget method with delegates before where the results were "useful if available, but not required". Just remember that you have no completion guarantees with that method. In particular, here's one place that I use it:

  • Start a delegate to check for application updates
  • Delegate begins a web request with a timeout
  • If an error/timeout occurs, or if the application is up-to-date, the method simply returns
  • If the application is out of date, I place a non-focus-stealing systray message stating so (no systray icon unless the update is available)

Either way, the application continues uninterrupted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文