WorkItemFactory.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext;
  78. workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback;
  79. workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute;
  80. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  81. workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority;
  82. WorkItem workItem = new WorkItem(
  83. workItemsGroup,
  84. workItemInfo,
  85. callback,
  86. state);
  87. return workItem;
  88. }
  89. /// <summary>
  90. /// Create a new work item
  91. /// </summary>
  92. /// <param name="workItemsGroup">The work items group</param>
  93. /// <param name="wigStartInfo">Work item group start information</param>
  94. /// <param name="callback">A callback to execute</param>
  95. /// <param name="state">
  96. /// The context object of the work item. Used for passing arguments to the work item.
  97. /// </param>
  98. /// <param name="workItemPriority">The work item priority</param>
  99. /// <returns>Returns a work item</returns>
  100. public static WorkItem CreateWorkItem(
  101. IWorkItemsGroup workItemsGroup,
  102. WIGStartInfo wigStartInfo,
  103. WorkItemCallback callback,
  104. object state,
  105. WorkItemPriority workItemPriority)
  106. {
  107. ValidateCallback(callback);
  108. WorkItemInfo workItemInfo = new WorkItemInfo();
  109. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  110. workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext;
  111. workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback;
  112. workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute;
  113. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  114. workItemInfo.WorkItemPriority = workItemPriority;
  115. WorkItem workItem = new WorkItem(
  116. workItemsGroup,
  117. workItemInfo,
  118. callback,
  119. state);
  120. return workItem;
  121. }
  122. /// <summary>
  123. /// Create a new work item
  124. /// </summary>
  125. /// <param name="workItemsGroup">The work items group</param>
  126. /// <param name="wigStartInfo">Work item group start information</param>
  127. /// <param name="workItemInfo">Work item information</param>
  128. /// <param name="callback">A callback to execute</param>
  129. /// <param name="state">
  130. /// The context object of the work item. Used for passing arguments to the work item.
  131. /// </param>
  132. /// <returns>Returns a work item</returns>
  133. public static WorkItem CreateWorkItem(
  134. IWorkItemsGroup workItemsGroup,
  135. WIGStartInfo wigStartInfo,
  136. WorkItemInfo workItemInfo,
  137. WorkItemCallback callback,
  138. object state)
  139. {
  140. ValidateCallback(callback);
  141. ValidateCallback(workItemInfo.PostExecuteWorkItemCallback);
  142. WorkItem workItem = new WorkItem(
  143. workItemsGroup,
  144. new WorkItemInfo(workItemInfo),
  145. callback,
  146. state);
  147. return workItem;
  148. }
  149. /// <summary>
  150. /// Create a new work item
  151. /// </summary>
  152. /// <param name="workItemsGroup">The work items group</param>
  153. /// <param name="wigStartInfo">Work item group start information</param>
  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</returns>
  162. public static WorkItem CreateWorkItem(
  163. IWorkItemsGroup workItemsGroup,
  164. WIGStartInfo wigStartInfo,
  165. WorkItemCallback callback,
  166. object state,
  167. PostExecuteWorkItemCallback postExecuteWorkItemCallback)
  168. {
  169. ValidateCallback(callback);
  170. ValidateCallback(postExecuteWorkItemCallback);
  171. WorkItemInfo workItemInfo = new WorkItemInfo();
  172. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  173. workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext;
  174. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  175. workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute;
  176. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  177. workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority;
  178. WorkItem workItem = new WorkItem(
  179. workItemsGroup,
  180. workItemInfo,
  181. callback,
  182. state);
  183. return workItem;
  184. }
  185. /// <summary>
  186. /// Create a new work item
  187. /// </summary>
  188. /// <param name="workItemsGroup">The work items group</param>
  189. /// <param name="wigStartInfo">Work item group start information</param>
  190. /// <param name="callback">A callback to execute</param>
  191. /// <param name="state">
  192. /// The context object of the work item. Used for passing arguments to the work item.
  193. /// </param>
  194. /// <param name="postExecuteWorkItemCallback">
  195. /// A delegate to call after the callback completion
  196. /// </param>
  197. /// <param name="workItemPriority">The work item priority</param>
  198. /// <returns>Returns a work item</returns>
  199. public static WorkItem CreateWorkItem(
  200. IWorkItemsGroup workItemsGroup,
  201. WIGStartInfo wigStartInfo,
  202. WorkItemCallback callback,
  203. object state,
  204. PostExecuteWorkItemCallback postExecuteWorkItemCallback,
  205. WorkItemPriority workItemPriority)
  206. {
  207. ValidateCallback(callback);
  208. ValidateCallback(postExecuteWorkItemCallback);
  209. WorkItemInfo workItemInfo = new WorkItemInfo();
  210. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  211. workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext;
  212. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  213. workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute;
  214. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  215. workItemInfo.WorkItemPriority = workItemPriority;
  216. WorkItem workItem = new WorkItem(
  217. workItemsGroup,
  218. workItemInfo,
  219. callback,
  220. state);
  221. return workItem;
  222. }
  223. /// <summary>
  224. /// Create a new work item
  225. /// </summary>
  226. /// <param name="workItemsGroup">The work items group</param>
  227. /// <param name="wigStartInfo">Work item group start information</param>
  228. /// <param name="callback">A callback to execute</param>
  229. /// <param name="state">
  230. /// The context object of the work item. Used for passing arguments to the work item.
  231. /// </param>
  232. /// <param name="postExecuteWorkItemCallback">
  233. /// A delegate to call after the callback completion
  234. /// </param>
  235. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  236. /// <returns>Returns a work item</returns>
  237. public static WorkItem CreateWorkItem(
  238. IWorkItemsGroup workItemsGroup,
  239. WIGStartInfo wigStartInfo,
  240. WorkItemCallback callback,
  241. object state,
  242. PostExecuteWorkItemCallback postExecuteWorkItemCallback,
  243. CallToPostExecute callToPostExecute)
  244. {
  245. ValidateCallback(callback);
  246. ValidateCallback(postExecuteWorkItemCallback);
  247. WorkItemInfo workItemInfo = new WorkItemInfo();
  248. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  249. workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext;
  250. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  251. workItemInfo.CallToPostExecute = callToPostExecute;
  252. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  253. workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority;
  254. WorkItem workItem = new WorkItem(
  255. workItemsGroup,
  256. workItemInfo,
  257. callback,
  258. state);
  259. return workItem;
  260. }
  261. /// <summary>
  262. /// Create a new work item
  263. /// </summary>
  264. /// <param name="workItemsGroup">The work items group</param>
  265. /// <param name="wigStartInfo">Work item group start information</param>
  266. /// <param name="callback">A callback to execute</param>
  267. /// <param name="state">
  268. /// The context object of the work item. Used for passing arguments to the work item.
  269. /// </param>
  270. /// <param name="postExecuteWorkItemCallback">
  271. /// A delegate to call after the callback completion
  272. /// </param>
  273. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  274. /// <param name="workItemPriority">The work item priority</param>
  275. /// <returns>Returns a work item</returns>
  276. public static WorkItem CreateWorkItem(
  277. IWorkItemsGroup workItemsGroup,
  278. WIGStartInfo wigStartInfo,
  279. WorkItemCallback callback,
  280. object state,
  281. PostExecuteWorkItemCallback postExecuteWorkItemCallback,
  282. CallToPostExecute callToPostExecute,
  283. WorkItemPriority workItemPriority)
  284. {
  285. ValidateCallback(callback);
  286. ValidateCallback(postExecuteWorkItemCallback);
  287. WorkItemInfo workItemInfo = new WorkItemInfo();
  288. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  289. workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext;
  290. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  291. workItemInfo.CallToPostExecute = callToPostExecute;
  292. workItemInfo.WorkItemPriority = workItemPriority;
  293. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  294. WorkItem workItem = new WorkItem(
  295. workItemsGroup,
  296. workItemInfo,
  297. callback,
  298. state);
  299. return workItem;
  300. }
  301. private static void ValidateCallback(Delegate callback)
  302. {
  303. if (callback != null && callback.GetInvocationList().Length > 1)
  304. {
  305. throw new NotSupportedException("SmartThreadPool doesn't support delegates chains");
  306. }
  307. }
  308. }
  309. #endregion
  310. }