1
0

WorkItemFactory.cs 13 KB

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