Skip to content

Instantly share code, notes, and snippets.

@dbeattie71
Forked from DVDPT/gist:1607680
Created March 31, 2012 16:50
Show Gist options
  • Save dbeattie71/2266687 to your computer and use it in GitHub Desktop.
Save dbeattie71/2266687 to your computer and use it in GitHub Desktop.
RestSharp Async for windows phone 7
public static class RestSharpExtensions
{
public static Task<RestResponse<T>> ExecuteAsync<T>(this IRestClient client, IRestRequest request) where T : new()
{
return client.ExecuteAsync<T>(request, CancellationToken.None);
}
public static Task<RestResponse<T>> ExecuteAsync<T>(this IRestClient client, IRestRequest request, CancellationToken token) where T : new()
{
var taskCompletionSource = new TaskCompletionSource<RestResponse<T>>();
RestRequestAsyncHandle asyncHandle = null;
token.Register(() =>
{
if (asyncHandle != null)
asyncHandle.Abort();
taskCompletionSource.TrySetCanceled();
});
if (!token.IsCancellationRequested)
asyncHandle = client.ExecuteAsync(request, (RestResponse<T> response, RestRequestAsyncHandle handle) => taskCompletionSource.TrySetResult(response));
else
taskCompletionSource.TrySetCanceled();
return taskCompletionSource.Task;
}
public static Task<RestResponse> ExecuteAsync(this IRestClient client, IRestRequest request)
{
return client.ExecuteAsync(request, CancellationToken.None);
}
public static Task<RestResponse> ExecuteAsync(this IRestClient client, IRestRequest request, CancellationToken token)
{
var taskCompletionSource = new TaskCompletionSource<RestResponse>();
RestRequestAsyncHandle asyncHandle = null;
token.Register(() =>
{
if (asyncHandle != null)
asyncHandle.Abort();
taskCompletionSource.TrySetCanceled();
});
if (!token.IsCancellationRequested)
asyncHandle = client.ExecuteAsync(request, (response, handle) => taskCompletionSource.TrySetResult(response));
else
taskCompletionSource.TrySetCanceled();
return taskCompletionSource.Task;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment