简体   繁体   English

DownloadFileAsync vs DownloadFileTaskAsync

[英]DownloadFileAsync vs DownloadFileTaskAsync

  1. What is the difference of DownloadFileAsync and DownloadFileTaskAsync ? DownloadFileAsyncDownloadFileTaskAsync有什么区别?

  2. When I should use one instead of another? 当我应该使用一个而不是另一个? Any example would be appreciated. 任何一个例子将不胜感激。

General pattern - if you find two methods with names ending xxxAsync and xxxTaskAsync , then you should generally prefer the Task version. 常规模式 - 如果您发现两个名称以xxxAsyncxxxTaskAsync结尾的方法,那么您通常应该更喜欢Task版本。

The two versions will exist because the xxxAsync version was created before the Task-based Async Pattern (TAP) was introduced, and will be based on an older async pattern. 这两个版本将存在,因为xxxAsync版本是在引入基于任务的异步模式 (TAP)之前创建的,并且将基于较旧的异步模式。

When introducing TAP methods, the usual recommendation is to suffix the name with Async - but that cannot be done when there's already another method that has that same name 1 - so the recommendation then is to suffix with TaskAsync . 在引入TAP方法时,通常的建议是使用Async对名称进行后缀 - 但是当已经有另一个具有相同名称1的方法时无法完成 - 因此建议使用TaskAsync作为后缀。

In this specific case DownloadFileAsync is an implementation of the "Event-based Async Pattern" (EAP), which tends to be more awkward to work with. 在这种特定情况下, DownloadFileAsync“基于事件的异步模式” (EAP)的一种实现,它往往更难以使用。 That pattern, itself, superceded the original async pattern within the .NET Framework, which was based on matching Begin and End prefixed methods and IAsyncResult , the Async Programming Model 该模式本身取代了.NET Framework中的原始异步模式,该模式基于匹配BeginEnd前缀方法和IAsyncResult异步编程模型


1 In general, of course, you can introduce multiple methods with the same name, provided they have different signatures. 1当然,一般来说,只要它们具有不同的签名,您就可以引入多个具有相同名称的方法。 But when it comes to the async patterns, an EAP async method and a TAP async method will generally take the same arguments - they'll only differ on their return type. 但是当谈到异步模式时,EAP异步方法和TAP异步方法通常会采用相同的参数 - 它们只会在返回类型上有所不同。 Even if they did vary in their arguments, putting both patterns under exactly the same name would probably cause more confusion. 即使他们的论点确实不同,将两种模式置于完全相同的名称下也可能会引起更多的混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM