WorkItemFactory.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using System;
  2. namespace Amib.Threading.Internal
  3. {
  4. #region WorkItemFactory class
  5. public class WorkItemFactory
  6. {
  7. /// <summary>
  8. /// Create a new work item
  9. /// </summary>
  10. /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param>
  11. /// <param name="wigStartInfo">Work item group start information</param>
  12. /// <param name="callback">A callback to execute</param>
  13. /// <returns>Returns a work item</returns>
  14. public static WorkItem CreateWorkItem(
  15. IWorkItemsGroup workItemsGroup,
  16. WIGStartInfo wigStartInfo,
  17. WorkItemCallback callback)
  18. {
  19. return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null);
  20. }
  21. /// <summary>
  22. /// Create a new work item
  23. /// </summary>
  24. /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param>
  25. /// <param name="wigStartInfo">Work item group start information</param>
  26. /// <param name="callback">A callback to execute</param>
  27. /// <param name="workItemPriority">The priority of the work item</param>
  28. /// <returns>Returns a work item</returns>
  29. public static WorkItem CreateWorkItem(
  30. IWorkItemsGroup workItemsGroup,
  31. WIGStartInfo wigStartInfo,
  32. WorkItemCallback callback,
  33. WorkItemPriority workItemPriority)
  34. {
  35. return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null, workItemPriority);
  36. }
  37. /// <summary>
  38. /// Create a new work item
  39. /// </summary>
  40. /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param>
  41. /// <param name="wigStartInfo">Work item group start information</param>
  42. /// <param name="workItemInfo">Work item info</param>
  43. /// <param name="callback">A callback to execute</param>
  44. /// <returns>Returns a work item</returns>
  45. public static WorkItem CreateWorkItem(
  46. IWorkItemsGroup workItemsGroup,
  47. WIGStartInfo wigStartInfo,
  48. WorkItemInfo workItemInfo,
  49. WorkItemCallback callback)
  50. {
  51. return CreateWorkItem(
  52. workItemsGroup,
  53. wigStartInfo,
  54. workItemInfo,
  55. callback,
  56. null);
  57. }
  58. /// <summary>
  59. /// Create a new work item
  60. /// </summary>
  61. /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param>
  62. /// <param name="wigStartInfo">Work item group start information</param>
  63. /// <param name="callback">A callback to execute</param>
  64. /// <param name="state">
  65. /// The context object of the work item. Used for passing arguments to the work item.
  66. /// </param>
  67. /// <returns>Returns a work item</returns>
  68. public static WorkItem CreateWorkItem(
  69. IWorkItemsGroup workItemsGroup,
  70. WIGStartInfo wigStartInfo,
  71. WorkItemCallback callback,
  72. object state)
  73. {
  74. ValidateCallback(callback);
  75. WorkItemInfo workItemInfo = new WorkItemInfo();
  76. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  77. workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback;
  78. workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute;
  79. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  80. workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority;
  81. WorkItem workItem = new WorkItem(
  82. workItemsGroup,
  83. workItemInfo,
  84. callback,
  85. state);
  86. return workItem;
  87. }
  88. /// <summary>
  89. /// Create a new work item
  90. /// </summary>
  91. /// <param name="workItemsGroup">The work items group</param>
  92. /// <param name="wigStartInfo">Work item group start information</param>
  93. /// <param name="callback">A callback to execute</param>
  94. /// <param name="state">
  95. /// The context object of the work item. Used for passing arguments to the work item.
  96. /// </param>
  97. /// <param name="workItemPriority">The work item priority</param>
  98. /// <returns>Returns a work item</returns>
  99. public static WorkItem CreateWorkItem(
  100. IWorkItemsGroup workItemsGroup,
  101. WIGStartInfo wigStartInfo,
  102. WorkItemCallback callback,
  103. object state,
  104. WorkItemPriority workItemPriority)
  105. {
  106. ValidateCallback(callback);
  107. WorkItemInfo workItemInfo = new WorkItemInfo();
  108. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  109. workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback;
  110. workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute;
  111. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  112. workItemInfo.WorkItemPriority = workItemPriority;
  113. WorkItem workItem = new WorkItem(
  114. workItemsGroup,
  115. workItemInfo,
  116. callback,
  117. state);
  118. return workItem;
  119. }
  120. /// <summary>
  121. /// Create a new work item
  122. /// </summary>
  123. /// <param name="workItemsGroup">The work items group</param>
  124. /// <param name="wigStartInfo">Work item group start information</param>
  125. /// <param name="workItemInfo">Work item information</param>
  126. /// <param name="callback">A callback to execute</param>
  127. /// <param name="state">
  128. /// The context object of the work item. Used for passing arguments to the work item.
  129. /// </param>
  130. /// <returns>Returns a work item</returns>
  131. public static WorkItem CreateWorkItem(
  132. IWorkItemsGroup workItemsGroup,
  133. WIGStartInfo wigStartInfo,
  134. WorkItemInfo workItemInfo,
  135. WorkItemCallback callback,
  136. object state)
  137. {
  138. ValidateCallback(callback);
  139. ValidateCallback(workItemInfo.PostExecuteWorkItemCallback);
  140. WorkItem workItem = new WorkItem(
  141. workItemsGroup,
  142. new WorkItemInfo(workItemInfo),
  143. callback,
  144. state);
  145. return workItem;
  146. }
  147. /// <summary>
  148. /// Create a new work item
  149. /// </summary>
  150. /// <param name="workItemsGroup">The work items group</param>
  151. /// <param name="wigStartInfo">Work item group start information</param>
  152. /// <param name="callback">A callback to execute</param>
  153. /// <param name="state">
  154. /// The context object of the work item. Used for passing arguments to the work item.
  155. /// </param>
  156. /// <param name="postExecuteWorkItemCallback">
  157. /// A delegate to call after the callback completion
  158. /// </param>
  159. /// <returns>Returns a work item</returns>
  160. public static WorkItem CreateWorkItem(
  161. IWorkItemsGroup workItemsGroup,
  162. WIGStartInfo wigStartInfo,
  163. WorkItemCallback callback,
  164. object state,
  165. PostExecuteWorkItemCallback postExecuteWorkItemCallback)
  166. {
  167. ValidateCallback(callback);
  168. ValidateCallback(postExecuteWorkItemCallback);
  169. WorkItemInfo workItemInfo = new WorkItemInfo();
  170. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  171. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  172. workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute;
  173. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  174. workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority;
  175. WorkItem workItem = new WorkItem(
  176. workItemsGroup,
  177. workItemInfo,
  178. callback,
  179. state);
  180. return workItem;
  181. }
  182. /// <summary>
  183. /// Create a new work item
  184. /// </summary>
  185. /// <param name="workItemsGroup">The work items group</param>
  186. /// <param name="wigStartInfo">Work item group start information</param>
  187. /// <param name="callback">A callback to execute</param>
  188. /// <param name="state">
  189. /// The context object of the work item. Used for passing arguments to the work item.
  190. /// </param>
  191. /// <param name="postExecuteWorkItemCallback">
  192. /// A delegate to call after the callback completion
  193. /// </param>
  194. /// <param name="workItemPriority">The work item priority</param>
  195. /// <returns>Returns a work item</returns>
  196. public static WorkItem CreateWorkItem(
  197. IWorkItemsGroup workItemsGroup,
  198. WIGStartInfo wigStartInfo,
  199. WorkItemCallback callback,
  200. object state,
  201. PostExecuteWorkItemCallback postExecuteWorkItemCallback,
  202. WorkItemPriority workItemPriority)
  203. {
  204. ValidateCallback(callback);
  205. ValidateCallback(postExecuteWorkItemCallback);
  206. WorkItemInfo workItemInfo = new WorkItemInfo();
  207. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  208. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  209. workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute;
  210. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  211. workItemInfo.WorkItemPriority = workItemPriority;
  212. WorkItem workItem = new WorkItem(
  213. workItemsGroup,
  214. workItemInfo,
  215. callback,
  216. state);
  217. return workItem;
  218. }
  219. /// <summary>
  220. /// Create a new work item
  221. /// </summary>
  222. /// <param name="workItemsGroup">The work items group</param>
  223. /// <param name="wigStartInfo">Work item group start information</param>
  224. /// <param name="callback">A callback to execute</param>
  225. /// <param name="state">
  226. /// The context object of the work item. Used for passing arguments to the work item.
  227. /// </param>
  228. /// <param name="postExecuteWorkItemCallback">
  229. /// A delegate to call after the callback completion
  230. /// </param>
  231. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  232. /// <returns>Returns a work item</returns>
  233. public static WorkItem CreateWorkItem(
  234. IWorkItemsGroup workItemsGroup,
  235. WIGStartInfo wigStartInfo,
  236. WorkItemCallback callback,
  237. object state,
  238. PostExecuteWorkItemCallback postExecuteWorkItemCallback,
  239. CallToPostExecute callToPostExecute)
  240. {
  241. ValidateCallback(callback);
  242. ValidateCallback(postExecuteWorkItemCallback);
  243. WorkItemInfo workItemInfo = new WorkItemInfo();
  244. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  245. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  246. workItemInfo.CallToPostExecute = callToPostExecute;
  247. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  248. workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority;
  249. WorkItem workItem = new WorkItem(
  250. workItemsGroup,
  251. workItemInfo,
  252. callback,
  253. state);
  254. return workItem;
  255. }
  256. /// <summary>
  257. /// Create a new work item
  258. /// </summary>
  259. /// <param name="workItemsGroup">The work items group</param>
  260. /// <param name="wigStartInfo">Work item group start information</param>
  261. /// <param name="callback">A callback to execute</param>
  262. /// <param name="state">
  263. /// The context object of the work item. Used for passing arguments to the work item.
  264. /// </param>
  265. /// <param name="postExecuteWorkItemCallback">
  266. /// A delegate to call after the callback completion
  267. /// </param>
  268. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  269. /// <param name="workItemPriority">The work item priority</param>
  270. /// <returns>Returns a work item</returns>
  271. public static WorkItem CreateWorkItem(
  272. IWorkItemsGroup workItemsGroup,
  273. WIGStartInfo wigStartInfo,
  274. WorkItemCallback callback,
  275. object state,
  276. PostExecuteWorkItemCallback postExecuteWorkItemCallback,
  277. CallToPostExecute callToPostExecute,
  278. WorkItemPriority workItemPriority)
  279. {
  280. ValidateCallback(callback);
  281. ValidateCallback(postExecuteWorkItemCallback);
  282. WorkItemInfo workItemInfo = new WorkItemInfo();
  283. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  284. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  285. workItemInfo.CallToPostExecute = callToPostExecute;
  286. workItemInfo.WorkItemPriority = workItemPriority;
  287. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  288. WorkItem workItem = new WorkItem(
  289. workItemsGroup,
  290. workItemInfo,
  291. callback,
  292. state);
  293. return workItem;
  294. }
  295. private static void ValidateCallback(Delegate callback)
  296. {
  297. if (callback != null && callback.GetInvocationList().Length > 1)
  298. {
  299. throw new NotSupportedException("SmartThreadPool doesn't support delegates chains");
  300. }
  301. }
  302. }
  303. #endregion
  304. }