How to make Task awaitable

Viewed 17733

Yesterday I started playing with Microsoft CTP async library, and nowhere I could not find the proper implementation of the awaitable Task. I know that it must have implementation like this?:

public struct SampleAwaiter<T>
{
    private readonly Task<T> task;
    public SampleAwaiter(Task<T> task) { this.task = task; }
    public bool IsCompleted { get { return task.IsCompleted; } }
    public void OnCompleted(Action continuation) { TaskEx.Run(continuation); }
    public T GetResult() { return task.Result; }
}

But how would I now implement a task that would, let's say, wait 5 seconds, and the return some string, for example "Hello World"?

One way is to use Task directly like so:

Task<string> task = TaskEx.Run(
            () =>
                {
                    Thread.Sleep(5000);
                    return "Hello World";
                });

        string str = await task;

But how would I do that with the awaitable implementation? Or did I just misunderstood everything?

Thanks for any information/help :)

3 Answers
Related