public static void RunWithTimeoutToken(TimeSpan timeout, Func <CancellationToken, Task> asyncOperation) { using (Concurrency.TimeoutToken timeoutToken = new Concurrency.TimeoutToken(timeout)) { CancellationToken token = timeoutToken.Token; using (Task task = asyncOperation(token)) { task.Wait(token); } } }
static IEnumerable <Task> _getResponseAsync(TaskCompletionSource <WebResponse> tcs, WebRequest request, TimeSpan timeout) { using (var cancellation_token = new Concurrency.TimeoutToken(timeout)) using (var registration_token = cancellation_token.Token.Register(() => { request.Abort(); })) { using (var task_get_response = request.GetResponseAsync()) { yield return(task_get_response); tcs.SetFromTask(task_get_response); yield break; } } }
static IEnumerable<Task> _getResponseAsync(TaskCompletionSource<WebResponse> tcs, WebRequest request, TimeSpan timeout) { using (var cancellation_token = new Concurrency.TimeoutToken(timeout)) using (var registration_token = cancellation_token.Token.Register(() => { request.Abort(); })) { using (var task_get_response = request.GetResponseAsync()) { yield return task_get_response; tcs.SetFromTask(task_get_response); yield break; } } }