Interfaces.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Ami Bar
  2. // [email protected]
  3. using System;
  4. using System.Threading;
  5. namespace Amib.Threading
  6. {
  7. #region Delegates
  8. /// <summary>
  9. /// A delegate that represents the method to run as the work item
  10. /// </summary>
  11. /// <param name="state">A state object for the method to run</param>
  12. public delegate object WorkItemCallback(object state);
  13. /// <summary>
  14. /// A delegate to call after the WorkItemCallback completed
  15. /// </summary>
  16. /// <param name="wir">The work item result object</param>
  17. public delegate void PostExecuteWorkItemCallback(IWorkItemResult wir);
  18. /// <summary>
  19. /// A delegate to call when a WorkItemsGroup becomes idle
  20. /// </summary>
  21. /// <param name="workItemsGroup">A reference to the WorkItemsGroup that became idle</param>
  22. public delegate void WorkItemsGroupIdleHandler(IWorkItemsGroup workItemsGroup);
  23. #endregion
  24. #region WorkItem Priority
  25. public enum WorkItemPriority
  26. {
  27. Lowest,
  28. BelowNormal,
  29. Normal,
  30. AboveNormal,
  31. Highest,
  32. }
  33. #endregion
  34. #region IHasWorkItemPriority interface
  35. public interface IHasWorkItemPriority
  36. {
  37. WorkItemPriority WorkItemPriority { get; }
  38. }
  39. #endregion
  40. #region IWorkItemsGroup interface
  41. /// <summary>
  42. /// IWorkItemsGroup interface
  43. /// </summary>
  44. public interface IWorkItemsGroup
  45. {
  46. /// <summary>
  47. /// Get/Set the name of the WorkItemsGroup
  48. /// </summary>
  49. string Name { get; set; }
  50. IWorkItemResult QueueWorkItem(WorkItemCallback callback);
  51. IWorkItemResult QueueWorkItem(WorkItemCallback callback, WorkItemPriority workItemPriority);
  52. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state);
  53. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, WorkItemPriority workItemPriority);
  54. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback);
  55. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, WorkItemPriority workItemPriority);
  56. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute);
  57. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority);
  58. IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback);
  59. IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback, object state);
  60. void WaitForIdle();
  61. bool WaitForIdle(TimeSpan timeout);
  62. bool WaitForIdle(int millisecondsTimeout);
  63. int WaitingCallbacks { get; }
  64. event WorkItemsGroupIdleHandler OnIdle;
  65. void Cancel();
  66. void Start();
  67. }
  68. #endregion
  69. #region CallToPostExecute enumerator
  70. [Flags]
  71. public enum CallToPostExecute
  72. {
  73. Never = 0x00,
  74. WhenWorkItemCanceled = 0x01,
  75. WhenWorkItemNotCanceled = 0x02,
  76. Always = WhenWorkItemCanceled | WhenWorkItemNotCanceled,
  77. }
  78. #endregion
  79. #region IWorkItemResult interface
  80. /// <summary>
  81. /// IWorkItemResult interface
  82. /// </summary>
  83. public interface IWorkItemResult
  84. {
  85. /// <summary>
  86. /// Get the result of the work item.
  87. /// If the work item didn't run yet then the caller waits.
  88. /// </summary>
  89. /// <returns>The result of the work item</returns>
  90. object GetResult();
  91. /// <summary>
  92. /// Get the result of the work item.
  93. /// If the work item didn't run yet then the caller waits until timeout.
  94. /// </summary>
  95. /// <returns>The result of the work item</returns>
  96. /// On timeout throws WorkItemTimeoutException
  97. object GetResult(
  98. int millisecondsTimeout,
  99. bool exitContext);
  100. /// <summary>
  101. /// Get the result of the work item.
  102. /// If the work item didn't run yet then the caller waits until timeout.
  103. /// </summary>
  104. /// <returns>The result of the work item</returns>
  105. /// On timeout throws WorkItemTimeoutException
  106. object GetResult(
  107. TimeSpan timeout,
  108. bool exitContext);
  109. void Abort();
  110. /// <summary>
  111. /// Get the result of the work item.
  112. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  113. /// </summary>
  114. /// <param name="millisecondsTimeout">Timeout in milliseconds, or -1 for infinite</param>
  115. /// <param name="exitContext">
  116. /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false.
  117. /// </param>
  118. /// <param name="cancelWaitHandle">A cancel wait handle to interrupt the blocking if needed</param>
  119. /// <returns>The result of the work item</returns>
  120. /// On timeout throws WorkItemTimeoutException
  121. /// On cancel throws WorkItemCancelException
  122. object GetResult(
  123. int millisecondsTimeout,
  124. bool exitContext,
  125. WaitHandle cancelWaitHandle);
  126. /// <summary>
  127. /// Get the result of the work item.
  128. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  129. /// </summary>
  130. /// <returns>The result of the work item</returns>
  131. /// On timeout throws WorkItemTimeoutException
  132. /// On cancel throws WorkItemCancelException
  133. object GetResult(
  134. TimeSpan timeout,
  135. bool exitContext,
  136. WaitHandle cancelWaitHandle);
  137. /// <summary>
  138. /// Get the result of the work item.
  139. /// If the work item didn't run yet then the caller waits.
  140. /// </summary>
  141. /// <param name="e">Filled with the exception if one was thrown</param>
  142. /// <returns>The result of the work item</returns>
  143. object GetResult(out Exception e);
  144. /// <summary>
  145. /// Get the result of the work item.
  146. /// If the work item didn't run yet then the caller waits until timeout.
  147. /// </summary>
  148. /// <param name="e">Filled with the exception if one was thrown</param>
  149. /// <returns>The result of the work item</returns>
  150. /// On timeout throws WorkItemTimeoutException
  151. object GetResult(
  152. int millisecondsTimeout,
  153. bool exitContext,
  154. out Exception e);
  155. /// <summary>
  156. /// Get the result of the work item.
  157. /// If the work item didn't run yet then the caller waits until timeout.
  158. /// </summary>
  159. /// <param name="e">Filled with the exception if one was thrown</param>
  160. /// <returns>The result of the work item</returns>
  161. /// On timeout throws WorkItemTimeoutException
  162. object GetResult(
  163. TimeSpan timeout,
  164. bool exitContext,
  165. out Exception e);
  166. /// <summary>
  167. /// Get the result of the work item.
  168. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  169. /// </summary>
  170. /// <param name="millisecondsTimeout">Timeout in milliseconds, or -1 for infinite</param>
  171. /// <param name="exitContext">
  172. /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false.
  173. /// </param>
  174. /// <param name="cancelWaitHandle">A cancel wait handle to interrupt the blocking if needed</param>
  175. /// <param name="e">Filled with the exception if one was thrown</param>
  176. /// <returns>The result of the work item</returns>
  177. /// On timeout throws WorkItemTimeoutException
  178. /// On cancel throws WorkItemCancelException
  179. object GetResult(
  180. int millisecondsTimeout,
  181. bool exitContext,
  182. WaitHandle cancelWaitHandle,
  183. out Exception e);
  184. /// <summary>
  185. /// Get the result of the work item.
  186. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  187. /// </summary>
  188. /// <returns>The result of the work item</returns>
  189. /// <param name="e">Filled with the exception if one was thrown</param>
  190. /// On timeout throws WorkItemTimeoutException
  191. /// On cancel throws WorkItemCancelException
  192. object GetResult(
  193. TimeSpan timeout,
  194. bool exitContext,
  195. WaitHandle cancelWaitHandle,
  196. out Exception e);
  197. /// <summary>
  198. /// Gets an indication whether the asynchronous operation has completed.
  199. /// </summary>
  200. bool IsCompleted { get; }
  201. /// <summary>
  202. /// Gets an indication whether the asynchronous operation has been canceled.
  203. /// </summary>
  204. bool IsCanceled { get; }
  205. /// <summary>
  206. /// Gets a user-defined object that qualifies or contains information about an asynchronous operation.
  207. /// </summary>
  208. object State { get; }
  209. /// <summary>
  210. /// Cancel the work item if it didn't start running yet.
  211. /// </summary>
  212. /// <returns>Returns true on success or false if the work item is in progress or already completed</returns>
  213. bool Cancel();
  214. /// <summary>
  215. /// Get the work item's priority
  216. /// </summary>
  217. WorkItemPriority WorkItemPriority { get; }
  218. /// <summary>
  219. /// Return the result, same as GetResult()
  220. /// </summary>
  221. object Result { get; }
  222. /// <summary>
  223. /// Returns the exception if occured otherwise returns null.
  224. /// </summary>
  225. object Exception { get; }
  226. }
  227. #endregion
  228. }