.net - 每个 BeginInvoke 之后必须跟一个 EndInvoke 吗?

标签 .net multithreading begininvoke

This page in the MS documentation ,涵盖了 Windows 窗体应用程序中的异步,指出:

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 , 陈述了一些不同的事情:

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



这两者似乎是直接冲突的。

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

另见,a post by Phil Haack .

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

最佳答案

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

但这条规则也有异常(exception)。 Control.BeginInvoke 等 API 不需要 EndInvoke,但它在文档中是明确的。

关于.net - 每个 BeginInvoke 之后必须跟一个 EndInvoke 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1274276/

相关文章:

c# - BeginInvoke、EndInvoke 的多线程问题?

c# - Dispatcher.Begin调用正确的用法?

.net - 如何使用 sendgrid 电子邮件发送嵌入的图像?

c# - Azure Durable Functions 中的最大事件响应数据大小限制

.net - NHibernate 和多个数据库后端 - 架构

.net - 了解 MSIL (CIL) 关键字中的点

c++ - 使用命令文件在线程之间传递消息

c - 对 `fprintf(stdout, ...)` 和 `fprintf(stderr, ...)` 的调用是否保证不会与多个线程交错?

java - 将数据写入控制台同时在java中打印信息

c# - 在 C# 中做一些工作时显示进度条?