Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadFileAsync vs DownloadFileTaskAsync

  1. What is the difference of DownloadFileAsync and DownloadFileTaskAsync?

  2. When I should use one instead of another? Any example would be appreciated.

like image 984
Endri Avatar asked Jan 05 '23 17:01

Endri


1 Answers

General pattern - if you find two methods with names ending xxxAsync and xxxTaskAsync, then you should generally prefer the Task version.

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.

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 name1 - so the recommendation then is to suffix with 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. 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


1In general, of course, you can introduce multiple methods with the same name, provided they have different signatures. 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. Even if they did vary in their arguments, putting both patterns under exactly the same name would probably cause more confusion.

like image 66
Damien_The_Unbeliever Avatar answered Jan 07 '23 06:01

Damien_The_Unbeliever