I found myself wanting to implement an IAwaitable class (something that implements asynchronous calls without blocking threads).
I’ve got the most recent version of AsyncCTP installed, and the compiler is saying that I need an IsCompleted() member. Okay, so the CTP preview has moved on a little bit (I get that, like it’s a preview)
Question: What interface are the AsyncCTP language extensions expecting now?
Question: In all this I’m assuming that I can signal to the “IAwaitable” via a lamda/delegate? Is this possible? Do we call EndAwait? The intellisense suggests that you call EndAwait to retrieve the result… so that doesn’t sound right. Any ideas?
All of the examples I’ve found so far are for features that the AsyncCTP library has already implemented such as:
await new WebClient().DownloadStringTaskAsync(uri).ConfigureAwait(false);
from the 101 AsyncSamplesCS
Background:
I find myself on Jon Skeets page (again) looking at this example
using System;
class Test
{
static async void Main()
{
await new Awaitable();
}
}
class Awaitable
{
public Awaiter GetAwaiter()
{
return new Awaiter();
}
}
class Awaiter
{
public bool BeginAwait(Action continuation)
{
return false;
}
public int EndAwait()
{
return 1;
}
}
With the SP1 refresh, you need:
GetAwaiter()method (possibly but not necessarily an extension method) that returns something (Awaiterin your example) with all of:bool IsCompletedproperty (get)void OnCompleted(Action callback)GetResult()method which returnsvoid, or the desired outcome of the awaited operationHowever, I suggest you look at
TaskCompletionSource<T>– I looked at this, and it out-performed my naive implementation (here; obsolete). You can also use it forvoidtasks, by using something like aTaskCompletionSource<bool>(and exploit the fact that theTask<bool>is also an untypedTask).