时间:2019-03-17 标签:c#asyncawaitimplementation
我对 C# 即将推出的 async/await 功能的设计有一些疑问。
- 将新机制附加到
Task
的便利性 - 我认为最好使用 async 代替 wait 关键字。 例如:
var result = async GetResultAsync();
- 使用 async 取消正在进行的异步操作的机制 token 并不像我想象的那么优雅。
Async/away 是一个很棒的功能,但我认为它的设计不如 LINQ。另外,我觉得设计团队对当前的设计非常满意;并且可能不会考虑社区反馈。
你怎么认为?
I have some doubts about the design of C#'s upcoming async/await feature.
- The convenience of attaching the new mechanism to
Task<T>
- I think is better to use async substituting the await keyword.
For expaple:var result = async GetResultAsync();
- The mechanism to cancel a ongoing async operation using a
token is not as elegant as I feel it could be.
Async/away is a great feature, but I think it's not as well designed as LINQ. Also, I feel the desing team is dangerously pleased with the current design; and might not take community feedback into account.
What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您尝试自己完成这项工作之前,此类设计决策往往没有什么意义。您的任务:编写异步代码并让它在您想要的时候停止。您的限制:您不能使用 Thread.Abort() 并且永远不会导致死锁。
These kind of design decisions tend to make little sense until you've tried to make this work yourself. Your assignment: write asynchronous code and make it stop whenever you want to. Your constraints: you cannot use Thread.Abort() and can never cause deadlock.
与 LINQ 的设计过程相比,您认为设计过程的哪些方面存在缺陷?
是什么给你这样的印象?一段时间以来,我们一直在邀请社区提供反馈。它并没有被忽视。
我认为这是一个讨论问题而不是工程问题。您确定这是回答该问题的正确网站吗?
What aspects of the design process do you feel are deficient compared to the design process for LINQ?
What gives you that impression? We've been inviting community feedback for quite some time now. It is not being ignored.
I think this is a discussion question and not an engineering question. Are you sure this is the right site for this question?
他们已经在
Task
和Task
上投入了大量的工作,其设计就是为了完美地适应这种情况。我发现这非常方便,因为我确切地知道对任务的期望 - 以前使用过它 - 而且我认为很多其他开发人员也会有这种感觉。在我看来,这比引入新的东西要好得多——而且我什至不确定有什么不同可以使新类型更能胜任这项任务。您有什么具体的想法吗?我认为您错过了
async
关键字的用途。它用于标记将(部分)异步执行的方法(当您使用await
关键字时)。编译器需要知道哪些方法要“执行魔法”;它不会将方法“转换”为异步执行。为此,您应该使用ThreadPool.QueueUserWorkItem
.
我无法对此发表评论。您认为更优雅的解决方案是什么?我相信,如果您与 Microsoft 分享反馈,Microsoft 会考虑您的反馈。
They have already invested a lot of work into
Task
andTask<T>
, which is designed to perfectly fit this situation. I find this very convenient, because I precisely know what to expect of a Task - having it used previously - and I think a lot of other developers will also feel that way. In my opinion it's a lot better than introducing something new - and I'm not even sure what could be different that would make a new type more up to the task. Do you have any concrete ideas?I think that you're missing the purpose of the
async
keyword. It is used to mark methods that will be (partially) executed asynchronously (when you use theawait
keyword). It is required for the compiler to know which methods to "perform magic on"; it doesn't "convert" a method to be executed asynchronously. For that you should useThreadPool.QueueUserWorkItem
.I can't really comment on this. What would be a more elegant solution in your opinion? I'm sure Microsoft will take your feedback into consideration if you share it with them.
开始阅读: http://blogs.msdn.com/b/ericlippert/archive/2010/10/28/asynchrony-in-c-5-part-one.aspx
Start reading: http://blogs.msdn.com/b/ericlippert/archive/2010/10/28/asynchrony-in-c-5-part-one.aspx