//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 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(); // // return 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(); // 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."); // } // } // } // } //}