Friday, November 22, 2013

What's TaskCompletionSource good for?

We can completely avoid TaskCompletionSource though if are able to call a method with await.


For example this method tries to get a value from a cache before resorting to doing an actual async operation:



private async static Task<IPAddress[]> resolve(string domain)
{
if (cache.ContainsKey(domain))
return(cache[domain]);

IPAddress[] result = await Dns.GetHostAddressesAsync(domain);

cache.TryAdd(domain,result);

return (result);
}

This gives us the same effect - namely allows us to control how a Task completes (and thus setting its result from a cached value) yet has no need for TaskCompletionSource.


So - it seems to me that we have no more need for TaskCompletionSource with C# 5 - we can achieve the same effect in other ways...


Cap'n


No comments:

Post a Comment