methods.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //using System;
  2. //using System.Collections.Generic;
  3. //using System.ComponentModel;
  4. //using System.Data;
  5. //using System.Drawing;
  6. //using System.Linq;
  7. //using System.Text;
  8. //using System.Threading.Tasks;
  9. //using System.Windows.Forms;
  10. //using System.Threading;
  11. //namespace HXX.Scanner.Common
  12. //{
  13. // public static class methods
  14. // {
  15. // //// 有返回值
  16. // //public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
  17. // //{
  18. // // using (var timeoutCancellationTokenSource = new CancellationTokenSource())
  19. // // {
  20. // // var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
  21. // // if (completedTask == task)
  22. // // {
  23. // // timeoutCancellationTokenSource.Cancel();
  24. // // return await task; // Very important in order to propagate exceptions
  25. // // }
  26. // // else
  27. // // {
  28. // // throw new TimeoutException("The operation has timed out.");
  29. // // }
  30. // // }
  31. // //}
  32. // // 有返回值
  33. // public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
  34. // {
  35. // using (var timeoutCancellationTokenSource = new CancellationTokenSource())
  36. // {
  37. // var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token)).ConfigureAwait(false);
  38. // if (completedTask == task)
  39. // {
  40. // timeoutCancellationTokenSource.Cancel();
  41. // return await task.ConfigureAwait(false); // Very important in order to propagate exceptions
  42. // }
  43. // else
  44. // {
  45. // throw new TimeoutException("The operation has timed out.");
  46. // }
  47. // }
  48. // }
  49. // //// 无返回值
  50. // //public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
  51. // //{
  52. // // using (var timeoutCancellationTokenSource = new CancellationTokenSource())
  53. // // {
  54. // // var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
  55. // // if (completedTask == task)
  56. // // {
  57. // // timeoutCancellationTokenSource.Cancel();
  58. // // await task; // Very important in order to propagate exceptions
  59. // // }
  60. // // else
  61. // // {
  62. // // throw new TimeoutException("The operation has timed out.");
  63. // // }
  64. // // }
  65. // //}
  66. // // 无返回值
  67. // public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
  68. // {
  69. // using (var timeoutCancellationTokenSource = new CancellationTokenSource())
  70. // {
  71. // var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token)).ConfigureAwait(false);
  72. // if (completedTask == task)
  73. // {
  74. // timeoutCancellationTokenSource.Cancel();
  75. // await task.ConfigureAwait(false); // Very important in order to propagate exceptions
  76. // }
  77. // else
  78. // {
  79. // throw new TimeoutException("The operation has timed out.");
  80. // }
  81. // }
  82. // }
  83. // }
  84. //}