c# - .NET 4.5 中不完整的异步任务有何影响?

标签 c# async-await

假设您有一个 Web 处理程序调用这样的异步方法:

var foo = await SomeMethod();

而且,由于编码不当(没有 CancellationToken、没有超时等),SomeMethod 永远不会完成。沮丧的用户反过来在她的浏览器上按下“停止”并前往酒吧。

假设这种情况发生在很多用户身上。

我知道我可以写一个超时来防止它永远等待,但如果我不这样做......会发生什么?这是内存泄漏吗?最终会被清理干净吗?最坏的情况是什么?

最佳答案

And SomeMethod never returns. The user cancels the request and goes to the pub.

它们根本不是一回事。

如果 SomeMethod 永远不会完成,那么你就有了内存泄漏。 You should never, ever, ever write code that does this.

OTOH,如果用户取消请求(取消 CancellationToken),则方法完成,可能会出现 OperationCanceledException

关于c# - .NET 4.5 中不完整的异步任务有何影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21059269/

相关文章:

c# - 在 SQL Server 远程访问方面需要帮助

c# - [AADSTS65001] : The user or administrator has not consented to use the application

javascript - Node.js 和 Express : wait for asynchronous operation before responding to HTTP request

microsoft-metro - 调试器无法识别等待方法返回值的变量

c# - 使用 ContinueWith 或 Async-Await 时的不同行为

c# - 如何在使用await async期间运行sql查询

c# - 如何将复选框值绑定(bind)到整数列表?

javascript - ASP.net javascript调用代码隐藏方法

C# 初学者 : Delete ALL between two characters in a string (Regex? )

c# - 我无法理解 await/async 究竟是如何工作的