Every method of [...]
WebClient
usesAsyncCallback
This is false. Simply use the methods of WebClient
that have Task
in their name, i.e.:
- DownloadFileTaskAsync
- DownloadStringTaskAsync
- DownloadDataTaskAsync
Then you can await
each of those tasks.
In the event that you're working with some other type that really doesn't have versions of its async methods that return a Task
, you can use Task.FromAsync
to generate a task from that asynchronous method.
If the asynchronous method is not compatible with FromAsync
, either because it fires an event, takes a callback, or you simply have some slight differences in how you wish to define the semantics of the task, you can use a TaskCompletionSource
to manually generate a Task
given any other method of asynchrony. Just create the TCS and set the result/exception/cancellation based on whatever mechanism the other asynchronous method provides.