| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //using System;
- //using System.Collections.Generic;
- //using System.ComponentModel;
- //using System.Data;
- //using System.Drawing;
- //using System.Linq;
- //using System.Text;
- //using System.Threading.Tasks;
- //using System.Windows.Forms;
- //using System.Threading;
- //namespace HXX.Scanner.Common
- //{
- // public static class methods
- // {
- // //// 有返回值
- // //public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
- // //{
- // // using (var timeoutCancellationTokenSource = new CancellationTokenSource())
- // // {
- // // var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
- // // if (completedTask == task)
- // // {
- // // timeoutCancellationTokenSource.Cancel();
- // // return await task; // Very important in order to propagate exceptions
- // // }
- // // else
- // // {
- // // throw new TimeoutException("The operation has timed out.");
- // // }
- // // }
- // //}
- // // 有返回值
- // public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
- // {
- // using (var timeoutCancellationTokenSource = new CancellationTokenSource())
- // {
- // var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token)).ConfigureAwait(false);
- // if (completedTask == task)
- // {
- // timeoutCancellationTokenSource.Cancel();
- // return await task.ConfigureAwait(false); // Very important in order to propagate exceptions
- // }
- // else
- // {
- // throw new TimeoutException("The operation has timed out.");
- // }
- // }
- // }
- // //// 无返回值
- // //public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
- // //{
- // // using (var timeoutCancellationTokenSource = new CancellationTokenSource())
- // // {
- // // var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
- // // if (completedTask == task)
- // // {
- // // timeoutCancellationTokenSource.Cancel();
- // // await task; // Very important in order to propagate exceptions
- // // }
- // // else
- // // {
- // // throw new TimeoutException("The operation has timed out.");
- // // }
- // // }
- // //}
- // // 无返回值
- // public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
- // {
- // using (var timeoutCancellationTokenSource = new CancellationTokenSource())
- // {
- // var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token)).ConfigureAwait(false);
- // if (completedTask == task)
- // {
- // timeoutCancellationTokenSource.Cancel();
- // await task.ConfigureAwait(false); // Very important in order to propagate exceptions
- // }
- // else
- // {
- // throw new TimeoutException("The operation has timed out.");
- // }
- // }
- // }
- // }
- //}
|