Interfaces.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 WorkItem Priority
  38. /// <summary>
  39. /// Defines the availeable priorities of a work item.
  40. /// The higher the priority a work item has, the sooner
  41. /// it will be executed.
  42. /// </summary>
  43. public enum WorkItemPriority
  44. {
  45. Lowest,
  46. BelowNormal,
  47. Normal,
  48. AboveNormal,
  49. Highest,
  50. }
  51. #endregion
  52. #region IWorkItemsGroup interface
  53. /// <summary>
  54. /// IWorkItemsGroup interface
  55. /// Created by SmartThreadPool.CreateWorkItemsGroup()
  56. /// </summary>
  57. public interface IWorkItemsGroup
  58. {
  59. /// <summary>
  60. /// Get/Set the name of the WorkItemsGroup
  61. /// </summary>
  62. string Name { get; set; }
  63. /// <summary>
  64. /// Get/Set the maximum number of workitem that execute cocurrency on the thread pool
  65. /// </summary>
  66. int Concurrency { get; set; }
  67. /// <summary>
  68. /// Get the number of work items waiting in the queue.
  69. /// </summary>
  70. int WaitingCallbacks { get; }
  71. /// <summary>
  72. /// Get an array with all the state objects of the currently running items.
  73. /// The array represents a snap shot and impact performance.
  74. /// </summary>
  75. object[] GetStates();
  76. /// <summary>
  77. /// Get the WorkItemsGroup start information
  78. /// </summary>
  79. WIGStartInfo WIGStartInfo { get; }
  80. /// <summary>
  81. /// Starts to execute work items
  82. /// </summary>
  83. void Start();
  84. /// <summary>
  85. /// Cancel all the work items.
  86. /// Same as Cancel(false)
  87. /// </summary>
  88. void Cancel();
  89. /// <summary>
  90. /// Cancel all work items using thread abortion
  91. /// </summary>
  92. /// <param name="abortExecution">True to stop work items by raising ThreadAbortException</param>
  93. void Cancel(bool abortExecution);
  94. /// <summary>
  95. /// Wait for all work item to complete.
  96. /// </summary>
  97. void WaitForIdle();
  98. /// <summary>
  99. /// Wait for all work item to complete, until timeout expired
  100. /// </summary>
  101. /// <param name="timeout">How long to wait for the work items to complete</param>
  102. /// <returns>Returns true if work items completed within the timeout, otherwise false.</returns>
  103. bool WaitForIdle(TimeSpan timeout);
  104. /// <summary>
  105. /// Wait for all work item to complete, until timeout expired
  106. /// </summary>
  107. /// <param name="millisecondsTimeout">How long to wait for the work items to complete in milliseconds</param>
  108. /// <returns>Returns true if work items completed within the timeout, otherwise false.</returns>
  109. bool WaitForIdle(int millisecondsTimeout);
  110. /// <summary>
  111. /// IsIdle is true when there are no work items running or queued.
  112. /// </summary>
  113. bool IsIdle { get; }
  114. /// <summary>
  115. /// This event is fired when all work items are completed.
  116. /// (When IsIdle changes to true)
  117. /// This event only work on WorkItemsGroup. On SmartThreadPool
  118. /// it throws the NotImplementedException.
  119. /// </summary>
  120. event WorkItemsGroupIdleHandler OnIdle;
  121. #region QueueWorkItem
  122. /// <summary>
  123. /// Queue a work item
  124. /// </summary>
  125. /// <param name="callback">A callback to execute</param>
  126. /// <returns>Returns a work item result</returns>
  127. IWorkItemResult QueueWorkItem(WorkItemCallback callback);
  128. /// <summary>
  129. /// Queue a work item
  130. /// </summary>
  131. /// <param name="callback">A callback to execute</param>
  132. /// <param name="workItemPriority">The priority of the work item</param>
  133. /// <returns>Returns a work item result</returns>
  134. IWorkItemResult QueueWorkItem(WorkItemCallback callback, WorkItemPriority workItemPriority);
  135. /// <summary>
  136. /// Queue a work item
  137. /// </summary>
  138. /// <param name="callback">A callback to execute</param>
  139. /// <param name="state">
  140. /// The context object of the work item. Used for passing arguments to the work item.
  141. /// </param>
  142. /// <returns>Returns a work item result</returns>
  143. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state);
  144. /// <summary>
  145. /// Queue a work item
  146. /// </summary>
  147. /// <param name="callback">A callback to execute</param>
  148. /// <param name="state">
  149. /// The context object of the work item. Used for passing arguments to the work item.
  150. /// </param>
  151. /// <param name="workItemPriority">The work item priority</param>
  152. /// <returns>Returns a work item result</returns>
  153. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, WorkItemPriority workItemPriority);
  154. /// <summary>
  155. /// Queue a work item
  156. /// </summary>
  157. /// <param name="callback">A callback to execute</param>
  158. /// <param name="state">
  159. /// The context object of the work item. Used for passing arguments to the work item.
  160. /// </param>
  161. /// <param name="postExecuteWorkItemCallback">
  162. /// A delegate to call after the callback completion
  163. /// </param>
  164. /// <returns>Returns a work item result</returns>
  165. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback);
  166. /// <summary>
  167. /// Queue a work item
  168. /// </summary>
  169. /// <param name="callback">A callback to execute</param>
  170. /// <param name="state">
  171. /// The context object of the work item. Used for passing arguments to the work item.
  172. /// </param>
  173. /// <param name="postExecuteWorkItemCallback">
  174. /// A delegate to call after the callback completion
  175. /// </param>
  176. /// <param name="workItemPriority">The work item priority</param>
  177. /// <returns>Returns a work item result</returns>
  178. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, WorkItemPriority workItemPriority);
  179. /// <summary>
  180. /// Queue a work item
  181. /// </summary>
  182. /// <param name="callback">A callback to execute</param>
  183. /// <param name="state">
  184. /// The context object of the work item. Used for passing arguments to the work item.
  185. /// </param>
  186. /// <param name="postExecuteWorkItemCallback">
  187. /// A delegate to call after the callback completion
  188. /// </param>
  189. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  190. /// <returns>Returns a work item result</returns>
  191. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute);
  192. /// <summary>
  193. /// Queue a work item
  194. /// </summary>
  195. /// <param name="callback">A callback to execute</param>
  196. /// <param name="state">
  197. /// The context object of the work item. Used for passing arguments to the work item.
  198. /// </param>
  199. /// <param name="postExecuteWorkItemCallback">
  200. /// A delegate to call after the callback completion
  201. /// </param>
  202. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  203. /// <param name="workItemPriority">The work item priority</param>
  204. /// <returns>Returns a work item result</returns>
  205. IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority);
  206. /// <summary>
  207. /// Queue a work item
  208. /// </summary>
  209. /// <param name="workItemInfo">Work item info</param>
  210. /// <param name="callback">A callback to execute</param>
  211. /// <returns>Returns a work item result</returns>
  212. IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback);
  213. /// <summary>
  214. /// Queue a work item
  215. /// </summary>
  216. /// <param name="workItemInfo">Work item information</param>
  217. /// <param name="callback">A callback to execute</param>
  218. /// <param name="state">
  219. /// The context object of the work item. Used for passing arguments to the work item.
  220. /// </param>
  221. /// <returns>Returns a work item result</returns>
  222. IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback, object state);
  223. #endregion
  224. #region QueueWorkItem(Action<...>)
  225. /// <summary>
  226. /// Queue a work item.
  227. /// </summary>
  228. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  229. IWorkItemResult QueueWorkItem(Action action);
  230. /// <summary>
  231. /// Queue a work item.
  232. /// </summary>
  233. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  234. IWorkItemResult QueueWorkItem (Action action, WorkItemPriority priority);
  235. /// <summary>
  236. /// Queue a work item.
  237. /// </summary>
  238. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  239. IWorkItemResult QueueWorkItem<T> (Action<T> action, T arg, WorkItemPriority priority);
  240. /// <summary>
  241. /// Queue a work item.
  242. /// </summary>
  243. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  244. IWorkItemResult QueueWorkItem<T> (Action<T> action, T arg);
  245. /// <summary>
  246. /// Queue a work item.
  247. /// </summary>
  248. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  249. IWorkItemResult QueueWorkItem<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2);
  250. /// <summary>
  251. /// Queue a work item.
  252. /// </summary>
  253. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  254. IWorkItemResult QueueWorkItem<T1, T2> (Action<T1, T2> action, T1 arg1, T2 arg2, WorkItemPriority priority);
  255. /// <summary>
  256. /// Queue a work item.
  257. /// </summary>
  258. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  259. IWorkItemResult QueueWorkItem<T1, T2, T3>(Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3);
  260. /// <summary>
  261. /// Queue a work item.
  262. /// </summary>
  263. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  264. IWorkItemResult QueueWorkItem<T1, T2, T3> (Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3, WorkItemPriority priority);
  265. /// <summary>
  266. /// Queue a work item.
  267. /// </summary>
  268. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  269. IWorkItemResult QueueWorkItem<T1, T2, T3, T4>(Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4);
  270. /// <summary>
  271. /// Queue a work item.
  272. /// </summary>
  273. /// <returns>Returns a IWorkItemResult object, but its GetResult() will always return null</returns>
  274. IWorkItemResult QueueWorkItem<T1, T2, T3, T4> (Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, WorkItemPriority priority);
  275. #endregion
  276. #region QueueWorkItem(Func<...>)
  277. /// <summary>
  278. /// Queue a work item.
  279. /// </summary>
  280. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  281. /// its GetResult() returns a TResult object</returns>
  282. IWorkItemResult<TResult> QueueWorkItem<TResult>(Func<TResult> func);
  283. /// <summary>
  284. /// Queue a work item.
  285. /// </summary>
  286. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  287. /// its GetResult() returns a TResult object</returns>
  288. IWorkItemResult<TResult> QueueWorkItem<T, TResult>(Func<T, TResult> func, T arg);
  289. /// <summary>
  290. /// Queue a work item.
  291. /// </summary>
  292. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  293. /// its GetResult() returns a TResult object</returns>
  294. IWorkItemResult<TResult> QueueWorkItem<T1, T2, TResult>(Func<T1, T2, TResult> func, T1 arg1, T2 arg2);
  295. /// <summary>
  296. /// Queue a work item.
  297. /// </summary>
  298. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  299. /// its GetResult() returns a TResult object</returns>
  300. IWorkItemResult<TResult> QueueWorkItem<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> func, T1 arg1, T2 arg2, T3 arg3);
  301. /// <summary>
  302. /// Queue a work item.
  303. /// </summary>
  304. /// <returns>Returns a IWorkItemResult&lt;TResult&gt; object.
  305. /// its GetResult() returns a TResult object</returns>
  306. IWorkItemResult<TResult> QueueWorkItem<T1, T2, T3, T4, TResult>(Func<T1, T2, T3, T4, TResult> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4);
  307. #endregion
  308. }
  309. #endregion
  310. #region CallToPostExecute enumerator
  311. [Flags]
  312. public enum CallToPostExecute
  313. {
  314. /// <summary>
  315. /// Never call to the PostExecute call back
  316. /// </summary>
  317. Never = 0x00,
  318. /// <summary>
  319. /// Call to the PostExecute only when the work item is cancelled
  320. /// </summary>
  321. WhenWorkItemCanceled = 0x01,
  322. /// <summary>
  323. /// Call to the PostExecute only when the work item is not cancelled
  324. /// </summary>
  325. WhenWorkItemNotCanceled = 0x02,
  326. /// <summary>
  327. /// Always call to the PostExecute
  328. /// </summary>
  329. Always = WhenWorkItemCanceled | WhenWorkItemNotCanceled,
  330. }
  331. #endregion
  332. #region IWorkItemResult interface
  333. /// <summary>
  334. /// The common interface of IWorkItemResult and IWorkItemResult&lt;T&gt;
  335. /// </summary>
  336. public interface IWaitableResult
  337. {
  338. /// <summary>
  339. /// This method intent is for internal use.
  340. /// </summary>
  341. /// <returns></returns>
  342. IWorkItemResult GetWorkItemResult();
  343. /// <summary>
  344. /// This method intent is for internal use.
  345. /// </summary>
  346. /// <returns></returns>
  347. IWorkItemResult<TResult> GetWorkItemResultT<TResult>();
  348. }
  349. /// <summary>
  350. /// IWorkItemResult interface.
  351. /// Created when a WorkItemCallback work item is queued.
  352. /// </summary>
  353. public interface IWorkItemResult : IWorkItemResult<object>
  354. {
  355. }
  356. /// <summary>
  357. /// IWorkItemResult&lt;TResult&gt; interface.
  358. /// Created when a Func&lt;TResult&gt; work item is queued.
  359. /// </summary>
  360. public interface IWorkItemResult<TResult> : IWaitableResult
  361. {
  362. /// <summary>
  363. /// Get the result of the work item.
  364. /// If the work item didn't run yet then the caller waits.
  365. /// </summary>
  366. /// <returns>The result of the work item</returns>
  367. TResult GetResult();
  368. /// <summary>
  369. /// Get the result of the work item.
  370. /// If the work item didn't run yet then the caller waits until timeout.
  371. /// </summary>
  372. /// <returns>The result of the work item</returns>
  373. /// On timeout throws WorkItemTimeoutException
  374. TResult GetResult(
  375. int millisecondsTimeout,
  376. bool exitContext);
  377. /// <summary>
  378. /// Get the result of the work item.
  379. /// If the work item didn't run yet then the caller waits until timeout.
  380. /// </summary>
  381. /// <returns>The result of the work item</returns>
  382. /// On timeout throws WorkItemTimeoutException
  383. TResult GetResult(
  384. TimeSpan timeout,
  385. bool exitContext);
  386. /// <summary>
  387. /// Get the result of the work item.
  388. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  389. /// </summary>
  390. /// <param name="millisecondsTimeout">Timeout in milliseconds, or -1 for infinite</param>
  391. /// <param name="exitContext">
  392. /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false.
  393. /// </param>
  394. /// <param name="cancelWaitHandle">A cancel wait handle to interrupt the blocking if needed</param>
  395. /// <returns>The result of the work item</returns>
  396. /// On timeout throws WorkItemTimeoutException
  397. /// On cancel throws WorkItemCancelException
  398. TResult GetResult(
  399. int millisecondsTimeout,
  400. bool exitContext,
  401. WaitHandle cancelWaitHandle);
  402. /// <summary>
  403. /// Get the result of the work item.
  404. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  405. /// </summary>
  406. /// <returns>The result of the work item</returns>
  407. /// On timeout throws WorkItemTimeoutException
  408. /// On cancel throws WorkItemCancelException
  409. TResult GetResult(
  410. TimeSpan timeout,
  411. bool exitContext,
  412. WaitHandle cancelWaitHandle);
  413. /// <summary>
  414. /// Get the result of the work item.
  415. /// If the work item didn't run yet then the caller waits.
  416. /// </summary>
  417. /// <param name="e">Filled with the exception if one was thrown</param>
  418. /// <returns>The result of the work item</returns>
  419. TResult GetResult(out Exception e);
  420. /// <summary>
  421. /// Get the result of the work item.
  422. /// If the work item didn't run yet then the caller waits until timeout.
  423. /// </summary>
  424. /// <param name="millisecondsTimeout"></param>
  425. /// <param name="exitContext"></param>
  426. /// <param name="e">Filled with the exception if one was thrown</param>
  427. /// <returns>The result of the work item</returns>
  428. /// On timeout throws WorkItemTimeoutException
  429. TResult GetResult(
  430. int millisecondsTimeout,
  431. bool exitContext,
  432. out Exception e);
  433. /// <summary>
  434. /// Get the result of the work item.
  435. /// If the work item didn't run yet then the caller waits until timeout.
  436. /// </summary>
  437. /// <param name="exitContext"></param>
  438. /// <param name="e">Filled with the exception if one was thrown</param>
  439. /// <param name="timeout"></param>
  440. /// <returns>The result of the work item</returns>
  441. /// On timeout throws WorkItemTimeoutException
  442. TResult GetResult(
  443. TimeSpan timeout,
  444. bool exitContext,
  445. out Exception e);
  446. /// <summary>
  447. /// Get the result of the work item.
  448. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  449. /// </summary>
  450. /// <param name="millisecondsTimeout">Timeout in milliseconds, or -1 for infinite</param>
  451. /// <param name="exitContext">
  452. /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false.
  453. /// </param>
  454. /// <param name="cancelWaitHandle">A cancel wait handle to interrupt the blocking if needed</param>
  455. /// <param name="e">Filled with the exception if one was thrown</param>
  456. /// <returns>The result of the work item</returns>
  457. /// On timeout throws WorkItemTimeoutException
  458. /// On cancel throws WorkItemCancelException
  459. TResult GetResult(
  460. int millisecondsTimeout,
  461. bool exitContext,
  462. WaitHandle cancelWaitHandle,
  463. out Exception e);
  464. /// <summary>
  465. /// Get the result of the work item.
  466. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled.
  467. /// </summary>
  468. /// <returns>The result of the work item</returns>
  469. /// <param name="cancelWaitHandle"></param>
  470. /// <param name="e">Filled with the exception if one was thrown</param>
  471. /// <param name="timeout"></param>
  472. /// <param name="exitContext"></param>
  473. /// On timeout throws WorkItemTimeoutException
  474. /// On cancel throws WorkItemCancelException
  475. TResult GetResult(
  476. TimeSpan timeout,
  477. bool exitContext,
  478. WaitHandle cancelWaitHandle,
  479. out Exception e);
  480. /// <summary>
  481. /// Gets an indication whether the asynchronous operation has completed.
  482. /// </summary>
  483. bool IsCompleted { get; }
  484. /// <summary>
  485. /// Gets an indication whether the asynchronous operation has been canceled.
  486. /// </summary>
  487. bool IsCanceled { get; }
  488. /// <summary>
  489. /// Gets the user-defined object that contains context data
  490. /// for the work item method.
  491. /// </summary>
  492. object State { get; }
  493. /// <summary>
  494. /// Same as Cancel(false).
  495. /// </summary>
  496. bool Cancel();
  497. /// <summary>
  498. /// Cancel the work item execution.
  499. /// If the work item is in the queue then it won't execute
  500. /// If the work item is completed, it will remain completed
  501. /// If the work item is in progress then the user can check the SmartThreadPool.IsWorkItemCanceled
  502. /// property to check if the work item has been cancelled. If the abortExecution is set to true then
  503. /// the Smart Thread Pool will send an AbortException to the running thread to stop the execution
  504. /// of the work item. When an in progress work item is canceled its GetResult will throw WorkItemCancelException.
  505. /// If the work item is already cancelled it will remain cancelled
  506. /// </summary>
  507. /// <param name="abortExecution">When true send an AbortException to the executing thread.</param>
  508. /// <returns>Returns true if the work item was not completed, otherwise false.</returns>
  509. bool Cancel(bool abortExecution);
  510. /// <summary>
  511. /// Get the work item's priority
  512. /// </summary>
  513. WorkItemPriority WorkItemPriority { get; }
  514. /// <summary>
  515. /// Return the result, same as GetResult()
  516. /// </summary>
  517. TResult Result { get; }
  518. /// <summary>
  519. /// Returns the exception if occured otherwise returns null.
  520. /// </summary>
  521. object Exception { get; }
  522. }
  523. #endregion
  524. #region .NET 3.5
  525. // All these delegate are built-in .NET 3.5
  526. // Comment/Remove them when compiling to .NET 3.5 to avoid ambiguity.
  527. public delegate void Action();
  528. public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
  529. public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
  530. public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
  531. public delegate TResult Func<TResult>();
  532. public delegate TResult Func<T, TResult>(T arg1);
  533. public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
  534. public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
  535. public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
  536. #endregion
  537. }