I've been playing around with this and I can see how to expose a Task<T> from a conventional EAP based implementation, that is how to expose the Begin/End semantics as a simplerTask<T> semantics.
However it all seems a bit odd - I mean if we were able to set the Result property of a Task object, there'd be no need forTaskCompletionSource would there?
Look at this simple example I put together:
using System; using System.Net; using System.Threading; using System.Threading.Tasks; namespace Junk1 { class Program { static void Main(string[] args) { Task a = resolve("oreilly.com"); a.Wait(); Thread.Sleep(-1); } private static Task<IPAddress[]> resolve (string domain) { TaskCompletionSource<IPAddress[]> completer = new TaskCompletionSource<IPAddress[]>(); Dns.BeginGetHostAddresses(domain, (iar) => { TaskCompletionSource<IPAddress[]> completion = (TaskCompletionSource<IPAddress[]>)(iar.AsyncState); try { completion.SetResult(Dns.EndGetHostAddresses(iar)); } catch(Exception exc) { completion.SetException(exc); } }, completer); return completer.Task; } } }
As you can see all we really do with TaskCompletionSourceis call SetResult or SetException so if these were directly settableon Task instance then we'd be fine and have no need for TaskCompletionSource; now is this true or doesTaskCompletionSource offer additional capabilities?
I'm just trying to really make sure I understand TaskCompletionSource that's all I'm trying to do by asking this question.
Cap'n