Task <AFHTTPRequestOperation> enqueueOperation(AFHTTPClient handler, AFHTTPRequestOperation operation, CancellationToken cancelToken, Action onCompleted, Action <Exception> onError) { var tcs = new TaskCompletionSource <AFHTTPRequestOperation>(); if (cancelToken.IsCancellationRequested) { tcs.SetCanceled(); return(tcs.Task); } bool completed = false; operation.SetDownloadProgressBlock((a, b, c) => { // NB: We're totally cheating here, we just happen to know // that we're guaranteed to have response headers after the // first time we get progress. if (completed) { return; } completed = true; tcs.SetResult(operation); }); operation.SetCompletionBlockWithSuccess( (op, _) => onCompleted(), (op, err) => { var ex = new ApplicationException(); ex.Data.Add("op", op); ex.Data.Add("err", err); onCompleted(); if (completed) { onError(ex); return; } // NB: Secret Handshake is Secret completed = true; tcs.SetException(ex); }); handler.EnqueueHTTPRequestOperation(operation); cancelToken.Register(() => { if (completed) { return; } completed = true; operation.Cancel(); tcs.SetCanceled(); }); return(tcs.Task); }
Task<AFHTTPRequestOperation> enqueueOperation(AFHTTPClient handler, AFHTTPRequestOperation operation, CancellationToken cancelToken) { var tcs = new TaskCompletionSource<AFHTTPRequestOperation>(); if (cancelToken.IsCancellationRequested) { tcs.SetCanceled(); return tcs.Task; } bool completed = false; operation.SetCompletionBlockWithSuccess( (op, _) => { if (completed) return; completed = true; tcs.SetResult(op); }, (op, err) => { if (completed) return; // NB: Secret Handshake is Secret completed = true; var ex = new ApplicationException(); ex.Data.Add("op", op); ex.Data.Add("err", err); tcs.SetException(ex); }); handler.EnqueueHTTPRequestOperation(operation); cancelToken.Register(() => { if (completed) return; completed = true; operation.Cancel(); tcs.SetCanceled(); }); return tcs.Task; }
Task<AFHTTPRequestOperation> enqueueOperation(AFHTTPClient handler, AFHTTPRequestOperation operation, CancellationToken cancelToken, Action onCompleted, Action<Exception> onError) { var tcs = new TaskCompletionSource<AFHTTPRequestOperation>(); if (cancelToken.IsCancellationRequested) { tcs.SetCanceled(); return tcs.Task; } bool completed = false; operation.SetDownloadProgressBlock((a, b, c) => { // NB: We're totally cheating here, we just happen to know // that we're guaranteed to have response headers after the // first time we get progress. if (completed) return; completed = true; tcs.SetResult(operation); }); operation.SetCompletionBlockWithSuccess( (op, _) => { if (!completed) { completed = true; tcs.SetResult(operation); } onCompleted(); }, (op, err) => { var ex = new ApplicationException(); ex.Data.Add("op", op); ex.Data.Add("err", err); onCompleted(); if (completed) { onError(ex); return; } // NB: Secret Handshake is Secret completed = true; tcs.SetException(ex); }); handler.EnqueueHTTPRequestOperation(operation); cancelToken.Register(() => { if (completed) return; completed = true; operation.Cancel(); tcs.SetCanceled(); }); return tcs.Task; }