Interfaces.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. using System;
  2. using System.Threading;
  3. namespace Amib.Threading
  4. {
  5. #region Delegates
  6. /// <summary>
  7. /// A delegate that represents the method to run as the work item
  8. /// </summary>
  9. /// <param name="state">A state object for the method to run</param>
  10. public delegate object WorkItemCallback(object state);
  11. /// <summary>
  12. /// A delegate to call after the WorkItemCallback completed
  13. /// </summary>
  14. /// <param name="wir">The work item result object</param>
  15. public delegate void PostExecuteWorkItemCallback(IWorkItemResult wir);
  16. /// <summary>
  17. /// A delegate to call after the WorkItemCallback completed
  18. /// </summary>
  19. /// <param name="wir">The work item result object</param>
  20. public delegate void PostExecuteWorkItemCallback<TResult>(IWorkItemResult<TResult> wir);
  21. /// <summary>
  22. /// A delegate to call when a WorkItemsGroup becomes idle
  23. /// </summary>
  24. /// <param name="workItemsGroup">A reference to the WorkItemsGroup that became idle</param>
  25. public delegate void WorkItemsGroupIdleHandler(IWorkItemsGroup workItemsGroup);
  26. /// <summary>
  27. /// A delegate to call after a thread is created, but before
  28. /// it's first use.
  29. /// </summary>
  30. public delegate void ThreadInitializationHandler();
  31. /// <summary>
  32. /// A delegate to call when a thread is about to exit, after
  33. /// it is no longer belong to the pool.
  34. /// </summary>
  35. public delegate void ThreadTerminationHandler();
  36. #endregion
  37. #region IWorkItemsGroup interface
  38. /// <summary>
  39. /// IWorkItemsGroup interface
  40. /// Created by SmartThreadPool.CreateWorkItemsGroup()
  41. /// </summary>
  42. public interface IWorkItemsGroup
  43. {
  44. /// <summary>
  45. /// Get/Set the name of the WorkItemsGroup
  46. /// </summary>
  47. string Name { get; set; }
  48. int localID { get; set; }
  49. /// <summary>
  50. /// Get/Set the maximum number of workitem that execute cocurrency on the thread pool
  51. /// </summary>
  52. int Concurrency { get; set; }
  53. /// <summary>
  54. /// Get the number of work items waiting in the queue.
  55. /// </summary>
  56. int WaitingCallbacks { get; }
  57. /// <summary>
  58. /// Get an array with all the state objects of the currently running items.
  59. /// The array represents a snap shot and impact performance.
  60. /// </summary>
  61. object[] GetStates();
  62. /// <summary>
  63. /// Get the WorkItemsGroup start information
  64. /// </summary>
  65. WIGStartInfo WIGStartInfo { get; }
  66. /// <summary>
  67. /// Starts to execute work items
  68. /// </summary>
  69. void Start();
  70. /// <summary>
  71. /// Cancel all the work items.
  72. /// Same as Cancel(false)
  73. /// </summary>
  74. void Cancel();
  75. /// <summary>
  76. /// Cancel all work items using thread abortion
  77. /// </summary>
  78. /// <param name="abortExecution">True to stop work items by raising ThreadAbortException</param>
  79. void Cancel(bool abortExecution);
  80. /// <summary>
  81. /// Wait for all work item to complete.
  82. /// </summary>
  83. void WaitForIdle();
  84. /// <summary>
  85. /// Wait for all work item to complete, until timeout expired
  86. /// </summary>
  87. /// <param name="timeout">How long to wait for the work items to complete</param>
  88. /// <returns>Returns true if work items completed within the timeout, otherwise false.</returns>
  89. bool WaitForIdle(TimeSpan timeout);
  90. /// <summary>
  91. /// Wait for all work item to complete, until timeout expired
  92. /// </summary>
  93. /// <param name="millisecondsTimeout">How long to wait for the work items to complete in milliseconds</param>
  94. /// <returns>Returns true if work items completed within the timeout, otherwise false.</returns>
  95. bool WaitForIdle(int millisecondsTimeout);
  96. /// <summary>
  97. /// IsIdle is true when there are no work items running or queued.
  98. /// </summary>
  99. bool IsIdle { get; }
  100. /// <summary>
  101. /// This event is fired when all work items are completed.
  102. /// (When IsIdle changes to true)
  103. /// This event only work on WorkItemsGroup. On SmartThreadPool
  104. /// it throws the NotImplementedException.
  105. /// </summary>
  106. event WorkItemsGroupIdleHandler OnIdle;
  107. #region QueueWorkItem
  108. IWorkItemResult QueueWorkItem(WaitCallback callback);
  109. IWorkItemResult QueueWorkItem(WaitCallback callback, object state);
  110. IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WaitCallback callback);
  111. IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WaitCallback callback, object state);
  112. /// <summary>
  113. /// Queue a work item
  114. /// </summary>
  115. /// <param name="callback">A callback to execute</param>
  116. /// <returns>Returns a work item result</returns>
  117. IWorkItemResult QueueWorkItem(WorkItemCallback callback);
  118. /// <summary>
  119. /// Queue a work item
  120. /// </summary>
  121. /// <param name="callback">A callback to execute</param>
  122. /// <param name="state">
  123. /// The context object of the work item. Used for passing arguments to the work item.
  124. /// </param>
  125. /// <returns>Returns a work item result</returns>
  126. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state);
  127. /// <summary>
  128. /// Queue a work item
  129. /// </summary>
  130. /// <param name="callback">A callback to execute</param>
  131. /// <param name="state">
  132. /// The context object of the work item. Used for passing arguments to the work item.
  133. /// </param>
  134. /// <param name="postExecuteWorkItemCallback">
  135. /// A delegate to call after the callback completion
  136. /// </param>
  137. /// <returns>Returns a work item result</returns>
  138. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback);
  139. /// <summary>
  140. /// Queue a work item
  141. /// </summary>
  142. /// <param name="callback">A callback to execute</param>
  143. /// <param name="state">
  144. /// The context object of the work item. Used for passing arguments to the work item.
  145. /// </param>
  146. /// <param name="postExecuteWorkItemCallback">
  147. /// A delegate to call after the callback completion
  148. /// </param>
  149. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  150. /// <returns>Returns a work item result</returns>
  151. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute);
  152. /// <summary>
  153. /// Queue a work item
  154. /// </summary>
  155. /// <param name="workItemInfo">Work item info</param>
  156. /// <param name="callback">A callback to execute</param>
  157. /// <returns>Returns a work item result</returns>
  158. IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback);
  159. /// <summary>
  160. /// Queue a work item
  161. /// </summary>
  162. /// <param name="workItemInfo">Work item information</param>
  163. /// <param name="callback">A callback to execute</param>
  164. /// <param name="state">
  165. /// The context object of the work item. Used for passing arguments to the work item.
  166. /// </param>
  167. /// <returns>Returns a work item result</returns>
  168. IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback, object state);
  169. #endregion
  170. #region QueueWorkItem(Action<...>)
  171. /// <summary>
  172. /// Queue a work item.
  173. /// </summary>
  174. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  175. IWorkItemResult QueueWorkItem(Action action);
  176. /// <summary>
  177. /// Queue a work item.
  178. /// </summary>
  179. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  180. IWorkItemResult QueueWorkItem<T>(Action<T> action, T arg);
  181. /// <summary>
  182. /// Queue a work item.
  183. /// </summary>
  184. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  185. IWorkItemResult QueueWorkItem<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2);
  186. /// <summary>
  187. /// Queue a work item.
  188. /// </summary>
  189. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  190. IWorkItemResult QueueWorkItem<T1, T2, T3>(Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3);
  191. /// <summary>
  192. /// Queue a work item.
  193. /// </summary>
  194. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  195. IWorkItemResult QueueWorkItem<T1, T2, T3, T4>(Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4);
  196. #endregion
  197. #region QueueWorkItem(Func<...>)
  198. /// <summary>
  199. /// Queue a work item.
  200. /// </summary>
  201. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  202. /// its GetResult() returns a TResult object</returns>
  203. IWorkItemResult<TResult> QueueWorkItem<TResult>(Func<TResult> func);
  204. /// <summary>
  205. /// Queue a work item.
  206. /// </summary>
  207. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  208. /// its GetResult() returns a TResult object</returns>
  209. IWorkItemResult<TResult> QueueWorkItem<T, TResult>(Func<T, TResult> func, T arg);
  210. /// <summary>
  211. /// Queue a work item.
  212. /// </summary>
  213. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  214. /// its GetResult() returns a TResult object</returns>
  215. IWorkItemResult<TResult> QueueWorkItem<T1, T2, TResult>(Func<T1, T2, TResult> func, T1 arg1, T2 arg2);
  216. /// <summary>
  217. /// Queue a work item.
  218. /// </summary>
  219. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  220. /// its GetResult() returns a TResult object</returns>
  221. IWorkItemResult<TResult> QueueWorkItem<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> func, T1 arg1, T2 arg2, T3 arg3);
  222. /// <summary>
  223. /// Queue a work item.
  224. /// </summary>
  225. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  226. /// its GetResult() returns a TResult object</returns>
  227. IWorkItemResult<TResult> QueueWorkItem<T1, T2, T3, T4, TResult>(Func<T1, T2, T3, T4, TResult> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4);
  228. #endregion
  229. }
  230. #endregion
  231. #region CallToPostExecute enumerator
  232. [Flags]
  233. public enum CallToPostExecute
  234. {
  235. /// <summary>
  236. /// Never call to the PostExecute call back
  237. /// </summary>
  238. Never = 0x00,
  239. /// <summary>
  240. /// Call to the PostExecute only when the work item is cancelled
  241. /// </summary>
  242. WhenWorkItemCanceled = 0x01,
  243. /// <summary>
  244. /// Call to the PostExecute only when the work item is not cancelled
  245. /// </summary>
  246. WhenWorkItemNotCanceled = 0x02,
  247. /// <summary>
  248. /// Always call to the PostExecute
  249. /// </summary>
  250. Always = WhenWorkItemCanceled | WhenWorkItemNotCanceled,
  251. }
  252. #endregion
  253. #region IWorkItemResult interface
  254. /// <summary>
  255. /// The common interface of IWorkItemResult and IWorkItemResult&lt;T&gt;
  256. /// </summary>
  257. public interface IWaitableResult
  258. {
  259. /// <summary>
  260. /// This method intent is for internal use.
  261. /// </summary>
  262. /// <returns></returns>
  263. IWorkItemResult GetWorkItemResult();
  264. /// <summary>
  265. /// This method intent is for internal use.
  266. /// </summary>
  267. /// <returns></returns>
  268. IWorkItemResult<TResult> GetWorkItemResultT<TResult>();
  269. }
  270. /// <summary>
  271. /// IWorkItemResult interface.
  272. /// Created when a WorkItemCallback work item is queued.
  273. /// </summary>
  274. public interface IWorkItemResult : IWorkItemResult<object>
  275. {
  276. }
  277. /// <summary>
  278. /// IWorkItemResult&lt;TResult&gt; interface.
  279. /// Created when a Func&lt;TResult&gt; work item is queued.
  280. /// </summary>
  281. public interface IWorkItemResult<TResult> : IWaitableResult
  282. {
  283. /// <summary>
  284. /// Get the result of the work item.
  285. /// If the work item didn't run yet then the caller waits.
  286. /// </summary>
  287. /// <returns>The result of the work item</returns>
  288. TResult GetResult();
  289. /// <summary>
  290. /// Get the result of the work item.
  291. /// If the work item didn't run yet then the caller waits until timeout.
  292. /// </summary>
  293. /// <returns>The result of the work item</returns>
  294. /// On timeout throws WorkItemTimeoutException
  295. TResult GetResult(
  296. int millisecondsTimeout,
  297. bool exitContext);
  298. /// <summary>
  299. /// Get the result of the work item.
  300. /// If the work item didn't run yet then the caller waits until timeout.
  301. /// </summary>
  302. /// <returns>The result of the work item</returns>
  303. /// On timeout throws WorkItemTimeoutException
  304. TResult GetResult(
  305. TimeSpan timeout,
  306. bool exitContext);
  307. /// <summary>
  308. /// Get the result of the work item.
  309. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  310. /// </summary>
  311. /// <param name="millisecondsTimeout">Timeout in milliseconds, or -1 for infinite</param>
  312. /// <param name="exitContext">
  313. /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false.
  314. /// </param>
  315. /// <param name="cancelWaitHandle">A cancel wait handle to interrupt the blocking if needed</param>
  316. /// <returns>The result of the work item</returns>
  317. /// On timeout throws WorkItemTimeoutException
  318. /// On cancel throws WorkItemCancelException
  319. TResult GetResult(
  320. int millisecondsTimeout,
  321. bool exitContext,
  322. WaitHandle cancelWaitHandle);
  323. /// <summary>
  324. /// Get the result of the work item.
  325. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  326. /// </summary>
  327. /// <returns>The result of the work item</returns>
  328. /// On timeout throws WorkItemTimeoutException
  329. /// On cancel throws WorkItemCancelException
  330. TResult GetResult(
  331. TimeSpan timeout,
  332. bool exitContext,
  333. WaitHandle cancelWaitHandle);
  334. /// <summary>
  335. /// Get the result of the work item.
  336. /// If the work item didn't run yet then the caller waits.
  337. /// </summary>
  338. /// <param name="e">Filled with the exception if one was thrown</param>
  339. /// <returns>The result of the work item</returns>
  340. TResult GetResult(out Exception e);
  341. /// <summary>
  342. /// Get the result of the work item.
  343. /// If the work item didn't run yet then the caller waits until timeout.
  344. /// </summary>
  345. /// <param name="millisecondsTimeout"></param>
  346. /// <param name="exitContext"></param>
  347. /// <param name="e">Filled with the exception if one was thrown</param>
  348. /// <returns>The result of the work item</returns>
  349. /// On timeout throws WorkItemTimeoutException
  350. TResult GetResult(
  351. int millisecondsTimeout,
  352. bool exitContext,
  353. out Exception e);
  354. /// <summary>
  355. /// Get the result of the work item.
  356. /// If the work item didn't run yet then the caller waits until timeout.
  357. /// </summary>
  358. /// <param name="exitContext"></param>
  359. /// <param name="e">Filled with the exception if one was thrown</param>
  360. /// <param name="timeout"></param>
  361. /// <returns>The result of the work item</returns>
  362. /// On timeout throws WorkItemTimeoutException
  363. TResult GetResult(
  364. TimeSpan timeout,
  365. bool exitContext,
  366. out Exception e);
  367. /// <summary>
  368. /// Get the result of the work item.
  369. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  370. /// </summary>
  371. /// <param name="millisecondsTimeout">Timeout in milliseconds, or -1 for infinite</param>
  372. /// <param name="exitContext">
  373. /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false.
  374. /// </param>
  375. /// <param name="cancelWaitHandle">A cancel wait handle to interrupt the blocking if needed</param>
  376. /// <param name="e">Filled with the exception if one was thrown</param>
  377. /// <returns>The result of the work item</returns>
  378. /// On timeout throws WorkItemTimeoutException
  379. /// On cancel throws WorkItemCancelException
  380. TResult GetResult(
  381. int millisecondsTimeout,
  382. bool exitContext,
  383. WaitHandle cancelWaitHandle,
  384. out Exception e);
  385. /// <summary>
  386. /// Get the result of the work item.
  387. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  388. /// </summary>
  389. /// <returns>The result of the work item</returns>
  390. /// <param name="cancelWaitHandle"></param>
  391. /// <param name="e">Filled with the exception if one was thrown</param>
  392. /// <param name="timeout"></param>
  393. /// <param name="exitContext"></param>
  394. /// On timeout throws WorkItemTimeoutException
  395. /// On cancel throws WorkItemCancelException
  396. TResult GetResult(
  397. TimeSpan timeout,
  398. bool exitContext,
  399. WaitHandle cancelWaitHandle,
  400. out Exception e);
  401. /// <summary>
  402. /// Gets an indication whether the asynchronous operation has completed.
  403. /// </summary>
  404. bool IsCompleted { get; }
  405. /// <summary>
  406. /// Gets an indication whether the asynchronous operation has been canceled.
  407. /// </summary>
  408. bool IsCanceled { get; }
  409. /// <summary>
  410. /// Gets the user-defined object that contains context data
  411. /// for the work item method.
  412. /// </summary>
  413. object State { get; }
  414. /// <summary>
  415. /// Same as Cancel(false).
  416. /// </summary>
  417. bool Cancel();
  418. /// <summary>
  419. /// Cancel the work item execution.
  420. /// If the work item is in the queue then it won't execute
  421. /// If the work item is completed, it will remain completed
  422. /// If the work item is in progress then the user can check the SmartThreadPool.IsWorkItemCanceled
  423. /// property to check if the work item has been cancelled. If the abortExecution is set to true then
  424. /// the Smart Thread Pool will send an AbortException to the running thread to stop the execution
  425. /// of the work item. When an in progress work item is canceled its GetResult will throw WorkItemCancelException.
  426. /// If the work item is already cancelled it will remain cancelled
  427. /// </summary>
  428. /// <param name="abortExecution">When true send an AbortException to the executing thread.</param>
  429. /// <returns>Returns true if the work item was not completed, otherwise false.</returns>
  430. bool Cancel(bool abortExecution);
  431. /// <summary>
  432. /// Return the result, same as GetResult()
  433. /// </summary>
  434. TResult Result { get; }
  435. /// <summary>
  436. /// Returns the exception if occured otherwise returns null.
  437. /// </summary>
  438. object Exception { get; }
  439. }
  440. #endregion
  441. }