Async.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Extensions.Logging;
  7. using SuperSocket.SocketBase.Logging;
  8. namespace SuperSocket.SocketBase
  9. {
  10. /// <summary>
  11. /// Async extension class
  12. /// </summary>
  13. public static class Async
  14. {
  15. /// <summary>
  16. /// Runs the specified task.
  17. /// </summary>
  18. /// <param name="logProvider">The log provider.</param>
  19. /// <param name="task">The task.</param>
  20. /// <returns></returns>
  21. public static Task AsyncRun(this ILoggerProvider logProvider, Action task)
  22. {
  23. return AsyncRun(logProvider, task, TaskCreationOptions.None);
  24. }
  25. /// <summary>
  26. /// Runs the specified task.
  27. /// </summary>
  28. /// <param name="logProvider">The log provider.</param>
  29. /// <param name="task">The task.</param>
  30. /// <param name="taskOption">The task option.</param>
  31. /// <returns></returns>
  32. public static Task AsyncRun(this ILoggerProvider logProvider, Action task, TaskCreationOptions taskOption)
  33. {
  34. return AsyncRun(logProvider, task, taskOption, null);
  35. }
  36. /// <summary>
  37. /// Runs the specified task.
  38. /// </summary>
  39. /// <param name="logProvider">The log provider.</param>
  40. /// <param name="task">The task.</param>
  41. /// <param name="exceptionHandler">The exception handler.</param>
  42. /// <returns></returns>
  43. public static Task AsyncRun(this ILoggerProvider logProvider, Action task, Action<Exception> exceptionHandler)
  44. {
  45. return AsyncRun(logProvider, task, TaskCreationOptions.None, exceptionHandler);
  46. }
  47. /// <summary>
  48. /// Runs the specified task.
  49. /// </summary>
  50. /// <param name="logProvider">The log provider.</param>
  51. /// <param name="task">The task.</param>
  52. /// <param name="taskOption">The task option.</param>
  53. /// <param name="exceptionHandler">The exception handler.</param>
  54. /// <returns></returns>
  55. public static Task AsyncRun(this ILoggerProvider logProvider, Action task, TaskCreationOptions taskOption, Action<Exception> exceptionHandler)
  56. {
  57. return Task.Factory.StartNew(task, taskOption).ContinueWith(t =>
  58. {
  59. if (exceptionHandler != null)
  60. exceptionHandler(t.Exception);
  61. else
  62. {
  63. for (var i = 0; i < t.Exception.InnerExceptions.Count; i++)
  64. {
  65. logProvider.Logger.LogError(t.Exception.InnerExceptions[i], t.Exception.InnerExceptions[i].Message);
  66. }
  67. }
  68. }, TaskContinuationOptions.OnlyOnFaulted);
  69. }
  70. /// <summary>
  71. /// Runs the specified task.
  72. /// </summary>
  73. /// <param name="logProvider">The log provider.</param>
  74. /// <param name="task">The task.</param>
  75. /// <param name="state">The state.</param>
  76. /// <returns></returns>
  77. public static Task AsyncRun(this ILoggerProvider logProvider, Action<object> task, object state)
  78. {
  79. return AsyncRun(logProvider, task, state, TaskCreationOptions.None);
  80. }
  81. /// <summary>
  82. /// Runs the specified task.
  83. /// </summary>
  84. /// <param name="logProvider">The log provider.</param>
  85. /// <param name="task">The task.</param>
  86. /// <param name="state">The state.</param>
  87. /// <param name="taskOption">The task option.</param>
  88. /// <returns></returns>
  89. public static Task AsyncRun(this ILoggerProvider logProvider, Action<object> task, object state, TaskCreationOptions taskOption)
  90. {
  91. return AsyncRun(logProvider, task, state, taskOption, null);
  92. }
  93. /// <summary>
  94. /// Runs the specified task.
  95. /// </summary>
  96. /// <param name="logProvider">The log provider.</param>
  97. /// <param name="task">The task.</param>
  98. /// <param name="state">The state.</param>
  99. /// <param name="exceptionHandler">The exception handler.</param>
  100. /// <returns></returns>
  101. public static Task AsyncRun(this ILoggerProvider logProvider, Action<object> task, object state, Action<Exception> exceptionHandler)
  102. {
  103. return AsyncRun(logProvider, task, state, TaskCreationOptions.None, exceptionHandler);
  104. }
  105. /// <summary>
  106. /// Runs the specified task.
  107. /// </summary>
  108. /// <param name="logProvider">The log provider.</param>
  109. /// <param name="task">The task.</param>
  110. /// <param name="state">The state.</param>
  111. /// <param name="taskOption">The task option.</param>
  112. /// <param name="exceptionHandler">The exception handler.</param>
  113. /// <returns></returns>
  114. public static Task AsyncRun(this ILoggerProvider logProvider, Action<object> task, object state, TaskCreationOptions taskOption, Action<Exception> exceptionHandler)
  115. {
  116. return Task.Factory.StartNew(task, state, taskOption).ContinueWith(t =>
  117. {
  118. if (exceptionHandler != null)
  119. exceptionHandler(t.Exception);
  120. else
  121. {
  122. for (var i = 0; i < t.Exception.InnerExceptions.Count; i++)
  123. {
  124. logProvider.Logger.LogError(t.Exception.InnerExceptions[i], t.Exception.InnerExceptions[i].Message);
  125. }
  126. }
  127. }, TaskContinuationOptions.OnlyOnFaulted);
  128. }
  129. }
  130. }