I want to use TaskCompletionSource to wrap MyService which is a simple service:
public static Task ProcessAsync(MyService
An alternative solution to i3arnon's answer would be:
public async static Task ProcessAsync(MyService service, int parameter)
{
var tcs = new TaskCompletionSource();
EventHandler callback =
(s, e) => tcs.SetResult(e.Result);
try
{
contacts.Completed += callback;
contacts.RunAsync(parameter);
return await tcs.Task;
}
finally
{
contacts.Completed -= callback;
}
}
However, this solution will have a compiler generated state machine. It will use more memory and CPU.