Friday, November 22, 2013

What's TaskCompletionSource good for?

Well, of course, if you already have a method which returns a task then TaskCompletionSource isn't needed. It's only needed in those scenarios where a task isn't readily available.


Speaking for GetHostAddessesAsync: this one is implemented in terms of Begin/EndGetHostAddresses, it uses FromAsync which you may find useful in those cases where you have only Begin/End methods:



var task = Task<IPAddress[]>.Factory.FromAsync(Dns.BeginGetHostAddresses, Dns.EndGetHostAddresses, "localhost", null);

The .NET framework itself uses TaskCompletionSource and FromAsync in many cases to implement async methods based on pre existing code.

No comments:

Post a Comment