Skip to content

Instantly share code, notes, and snippets.

@jrusbatch
Created April 30, 2014 18:23
Show Gist options
  • Save jrusbatch/a577fda6d64eda821172 to your computer and use it in GitHub Desktop.
Save jrusbatch/a577fda6d64eda821172 to your computer and use it in GitHub Desktop.
public static Task<BrokeredMessage> OnMessageAsyncWrapper(CancellationToken cancellationToken = default(CancellationToken))
{
var tcs = new TaskCompletionSource<BrokeredMessage>();
try
{
if (cancellationToken.IsCancellationRequested)
{
tcs.SetCanceled();
return tcs.Task;
}
OnMessageAsync(msg =>
{
tcs.TrySetResult(msg);
return tcs.Task;
});
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
return tcs.Task;
}
public static void OnMessageAsync(Func<BrokeredMessage, Task> callback)
{
callback(default(BrokeredMessage));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment