1
0

WorkItemsGroupBase.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using System;
  2. using System.Threading;
  3. namespace Amib.Threading.Internal
  4. {
  5. public abstract class WorkItemsGroupBase : IWorkItemsGroup
  6. {
  7. #region Private Fields
  8. /// <summary>
  9. /// Contains the name of this instance of SmartThreadPool.
  10. /// Can be changed by the user.
  11. /// </summary>
  12. private string _name = "WorkItemsGroupBase";
  13. public WorkItemsGroupBase()
  14. {
  15. IsIdle = true;
  16. }
  17. #endregion
  18. #region IWorkItemsGroup Members
  19. #region Public Methods
  20. /// <summary>
  21. /// Get/Set the name of the SmartThreadPool/WorkItemsGroup instance
  22. /// </summary>
  23. public string Name
  24. {
  25. get { return _name; }
  26. set { _name = value; }
  27. }
  28. public int localID { get; set;}
  29. #endregion
  30. #region Abstract Methods
  31. public abstract int Concurrency { get; set; }
  32. public abstract int WaitingCallbacks { get; }
  33. public abstract object[] GetStates();
  34. public abstract WIGStartInfo WIGStartInfo { get; }
  35. public abstract void Start();
  36. public abstract void Cancel(bool abortExecution);
  37. public abstract bool WaitForIdle(int millisecondsTimeout);
  38. public abstract event WorkItemsGroupIdleHandler OnIdle;
  39. internal abstract void Enqueue(WorkItem workItem);
  40. internal virtual void PreQueueWorkItem() { }
  41. #endregion
  42. #region Common Base Methods
  43. /// <summary>
  44. /// Cancel all the work items.
  45. /// Same as Cancel(false)
  46. /// </summary>
  47. public virtual void Cancel()
  48. {
  49. Cancel(false);
  50. }
  51. /// <summary>
  52. /// Wait for the SmartThreadPool/WorkItemsGroup to be idle
  53. /// </summary>
  54. public void WaitForIdle()
  55. {
  56. WaitForIdle(Timeout.Infinite);
  57. }
  58. /// <summary>
  59. /// Wait for the SmartThreadPool/WorkItemsGroup to be idle
  60. /// </summary>
  61. public bool WaitForIdle(TimeSpan timeout)
  62. {
  63. return WaitForIdle((int)timeout.TotalMilliseconds);
  64. }
  65. /// <summary>
  66. /// IsIdle is true when there are no work items running or queued.
  67. /// </summary>
  68. public bool IsIdle { get; protected set; }
  69. #endregion
  70. #region QueueWorkItem
  71. public IWorkItemResult QueueWorkItem(WaitCallback callback)
  72. {
  73. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, null);
  74. Enqueue(workItem);
  75. return workItem.GetWorkItemResult();
  76. }
  77. public IWorkItemResult QueueWorkItem(WaitCallback callback, object state)
  78. {
  79. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, state);
  80. Enqueue(workItem);
  81. return workItem.GetWorkItemResult();
  82. }
  83. public IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WaitCallback callback)
  84. {
  85. PreQueueWorkItem();
  86. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, workItemInfo, callback, null);
  87. Enqueue(workItem);
  88. return workItem.GetWorkItemResult();
  89. }
  90. public IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WaitCallback callback, object state)
  91. {
  92. PreQueueWorkItem();
  93. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, workItemInfo, callback, state);
  94. Enqueue(workItem);
  95. return workItem.GetWorkItemResult();
  96. }
  97. /// <summary>
  98. /// Queue a work item
  99. /// </summary>
  100. /// <param name="callback">A callback to execute</param>
  101. /// <returns>Returns a work item result</returns>
  102. public IWorkItemResult QueueWorkItem(WorkItemCallback callback)
  103. {
  104. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback);
  105. Enqueue(workItem);
  106. return workItem.GetWorkItemResult();
  107. }
  108. /// <summary>
  109. /// Queue a work item
  110. /// </summary>
  111. /// <param name="workItemInfo">Work item info</param>
  112. /// <param name="callback">A callback to execute</param>
  113. /// <returns>Returns a work item result</returns>
  114. public IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback)
  115. {
  116. PreQueueWorkItem();
  117. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, workItemInfo, callback);
  118. Enqueue(workItem);
  119. return workItem.GetWorkItemResult();
  120. }
  121. /// <summary>
  122. /// Queue a work item
  123. /// </summary>
  124. /// <param name="callback">A callback to execute</param>
  125. /// <param name="state">
  126. /// The context object of the work item. Used for passing arguments to the work item.
  127. /// </param>
  128. /// <returns>Returns a work item result</returns>
  129. public IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state)
  130. {
  131. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, state);
  132. Enqueue(workItem);
  133. return workItem.GetWorkItemResult();
  134. }
  135. /// <summary>
  136. /// Queue a work item
  137. /// </summary>
  138. /// <param name="workItemInfo">Work item information</param>
  139. /// <param name="callback">A callback to execute</param>
  140. /// <param name="state">
  141. /// The context object of the work item. Used for passing arguments to the work item.
  142. /// </param>
  143. /// <returns>Returns a work item result</returns>
  144. public IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback, object state)
  145. {
  146. PreQueueWorkItem();
  147. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, workItemInfo, callback, state);
  148. Enqueue(workItem);
  149. return workItem.GetWorkItemResult();
  150. }
  151. /// <summary>
  152. /// Queue a work item
  153. /// </summary>
  154. /// <param name="callback">A callback to execute</param>
  155. /// <param name="state">
  156. /// The context object of the work item. Used for passing arguments to the work item.
  157. /// </param>
  158. /// <param name="postExecuteWorkItemCallback">
  159. /// A delegate to call after the callback completion
  160. /// </param>
  161. /// <returns>Returns a work item result</returns>
  162. public IWorkItemResult QueueWorkItem( WorkItemCallback callback, object state,
  163. PostExecuteWorkItemCallback postExecuteWorkItemCallback)
  164. {
  165. PreQueueWorkItem();
  166. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, state, postExecuteWorkItemCallback);
  167. Enqueue(workItem);
  168. return workItem.GetWorkItemResult();
  169. }
  170. /// <summary>
  171. /// Queue a work item
  172. /// </summary>
  173. /// <param name="callback">A callback to execute</param>
  174. /// <param name="state">
  175. /// The context object of the work item. Used for passing arguments to the work item.
  176. /// </param>
  177. /// <param name="postExecuteWorkItemCallback">
  178. /// A delegate to call after the callback completion
  179. /// </param>
  180. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  181. /// <returns>Returns a work item result</returns>
  182. public IWorkItemResult QueueWorkItem( WorkItemCallback callback, object state,
  183. PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute)
  184. {
  185. PreQueueWorkItem();
  186. WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, state, postExecuteWorkItemCallback, callToPostExecute);
  187. Enqueue(workItem);
  188. return workItem.GetWorkItemResult();
  189. }
  190. #endregion
  191. #region QueueWorkItem(Action<...>)
  192. public IWorkItemResult QueueWorkItem (Action action)
  193. {
  194. PreQueueWorkItem ();
  195. WorkItem workItem = WorkItemFactory.CreateWorkItem (
  196. this,
  197. WIGStartInfo,
  198. delegate
  199. {
  200. action.Invoke ();
  201. return null;
  202. });
  203. Enqueue (workItem);
  204. return workItem.GetWorkItemResult ();
  205. }
  206. public IWorkItemResult QueueWorkItem<T> (Action<T> action, T arg)
  207. {
  208. PreQueueWorkItem ();
  209. WorkItem workItem = WorkItemFactory.CreateWorkItem (
  210. this,
  211. WIGStartInfo,
  212. state =>
  213. {
  214. action.Invoke (arg);
  215. return null;
  216. },
  217. WIGStartInfo.FillStateWithArgs ? new object[] { arg } : null);
  218. Enqueue (workItem);
  219. return workItem.GetWorkItemResult ();
  220. }
  221. public IWorkItemResult QueueWorkItem<T1, T2> (Action<T1, T2> action, T1 arg1, T2 arg2)
  222. {
  223. PreQueueWorkItem ();
  224. WorkItem workItem = WorkItemFactory.CreateWorkItem (
  225. this,
  226. WIGStartInfo,
  227. state =>
  228. {
  229. action.Invoke (arg1, arg2);
  230. return null;
  231. },
  232. WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2 } : null);
  233. Enqueue (workItem);
  234. return workItem.GetWorkItemResult ();
  235. }
  236. public IWorkItemResult QueueWorkItem<T1, T2, T3> (Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3)
  237. {
  238. PreQueueWorkItem ();
  239. WorkItem workItem = WorkItemFactory.CreateWorkItem (
  240. this,
  241. WIGStartInfo,
  242. state =>
  243. {
  244. action.Invoke (arg1, arg2, arg3);
  245. return null;
  246. },
  247. WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2, arg3 } : null);
  248. Enqueue (workItem);
  249. return workItem.GetWorkItemResult ();
  250. }
  251. public IWorkItemResult QueueWorkItem<T1, T2, T3, T4> (
  252. Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  253. {
  254. PreQueueWorkItem ();
  255. WorkItem workItem = WorkItemFactory.CreateWorkItem (
  256. this,
  257. WIGStartInfo,
  258. state =>
  259. {
  260. action.Invoke (arg1, arg2, arg3, arg4);
  261. return null;
  262. },
  263. WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2, arg3, arg4 } : null);
  264. Enqueue (workItem);
  265. return workItem.GetWorkItemResult ();
  266. }
  267. #endregion
  268. #region QueueWorkItem(Func<...>)
  269. public IWorkItemResult<TResult> QueueWorkItem<TResult>(Func<TResult> func)
  270. {
  271. PreQueueWorkItem();
  272. WorkItem workItem = WorkItemFactory.CreateWorkItem(
  273. this,
  274. WIGStartInfo,
  275. state =>
  276. {
  277. return func.Invoke();
  278. });
  279. Enqueue(workItem);
  280. return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
  281. }
  282. public IWorkItemResult<TResult> QueueWorkItem<T, TResult>(Func<T, TResult> func, T arg)
  283. {
  284. PreQueueWorkItem();
  285. WorkItem workItem = WorkItemFactory.CreateWorkItem(
  286. this,
  287. WIGStartInfo,
  288. state =>
  289. {
  290. return func.Invoke(arg);
  291. },
  292. WIGStartInfo.FillStateWithArgs ? new object[] { arg } : null);
  293. Enqueue(workItem);
  294. return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
  295. }
  296. public IWorkItemResult<TResult> QueueWorkItem<T1, T2, TResult>(Func<T1, T2, TResult> func, T1 arg1, T2 arg2)
  297. {
  298. PreQueueWorkItem();
  299. WorkItem workItem = WorkItemFactory.CreateWorkItem(
  300. this,
  301. WIGStartInfo,
  302. state =>
  303. {
  304. return func.Invoke(arg1, arg2);
  305. },
  306. WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2 } : null);
  307. Enqueue(workItem);
  308. return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
  309. }
  310. public IWorkItemResult<TResult> QueueWorkItem<T1, T2, T3, TResult>(
  311. Func<T1, T2, T3, TResult> func, T1 arg1, T2 arg2, T3 arg3)
  312. {
  313. PreQueueWorkItem();
  314. WorkItem workItem = WorkItemFactory.CreateWorkItem(
  315. this,
  316. WIGStartInfo,
  317. state =>
  318. {
  319. return func.Invoke(arg1, arg2, arg3);
  320. },
  321. WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2, arg3 } : null);
  322. Enqueue(workItem);
  323. return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
  324. }
  325. public IWorkItemResult<TResult> QueueWorkItem<T1, T2, T3, T4, TResult>(
  326. Func<T1, T2, T3, T4, TResult> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  327. {
  328. PreQueueWorkItem();
  329. WorkItem workItem = WorkItemFactory.CreateWorkItem(
  330. this,
  331. WIGStartInfo,
  332. state =>
  333. {
  334. return func.Invoke(arg1, arg2, arg3, arg4);
  335. },
  336. WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2, arg3, arg4 } : null);
  337. Enqueue(workItem);
  338. return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
  339. }
  340. #endregion
  341. #endregion
  342. }
  343. }