Async.cs 5.7 KB

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