WorkItemFactory.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Threading;
  3. namespace Amib.Threading.Internal
  4. {
  5. #region WorkItemFactory class
  6. public class WorkItemFactory
  7. {
  8. public static WorkItem CreateWorkItem(IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemInfo workItemInfo,
  9. WaitCallback callback, object state)
  10. {
  11. ValidateCallback(callback);
  12. ValidateCallback(workItemInfo.PostExecuteWorkItemCallback);
  13. WorkItem workItem = new WorkItem(
  14. workItemsGroup,
  15. new WorkItemInfo(workItemInfo),
  16. callback,
  17. state);
  18. return workItem;
  19. }
  20. public static WorkItem CreateWorkItem(IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo,
  21. WaitCallback callback, object state)
  22. {
  23. ValidateCallback(callback);
  24. WorkItemInfo workItemInfo = new WorkItemInfo()
  25. {
  26. UseCallerCallContext = wigStartInfo.UseCallerCallContext,
  27. PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback,
  28. CallToPostExecute = wigStartInfo.CallToPostExecute,
  29. DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects,
  30. };
  31. WorkItem workItem = new WorkItem(
  32. workItemsGroup,
  33. workItemInfo,
  34. callback,
  35. state);
  36. return workItem;
  37. }
  38. /// <summary>
  39. /// Create a new work item
  40. /// </summary>
  41. /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param>
  42. /// <param name="wigStartInfo">Work item group start information</param>
  43. /// <param name="callback">A callback to execute</param>
  44. /// <returns>Returns a work item</returns>
  45. public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemCallback callback)
  46. {
  47. return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null);
  48. }
  49. /// <summary>
  50. /// Create a new work item
  51. /// </summary>
  52. /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param>
  53. /// <param name="wigStartInfo">Work item group start information</param>
  54. /// <param name="workItemInfo">Work item info</param>
  55. /// <param name="callback">A callback to execute</param>
  56. /// <returns>Returns a work item</returns>
  57. public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo,
  58. WorkItemInfo workItemInfo, WorkItemCallback callback)
  59. {
  60. return CreateWorkItem(
  61. workItemsGroup,
  62. wigStartInfo,
  63. workItemInfo,
  64. callback,
  65. null);
  66. }
  67. /// <summary>
  68. /// Create a new work item
  69. /// </summary>
  70. /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param>
  71. /// <param name="wigStartInfo">Work item group start information</param>
  72. /// <param name="callback">A callback to execute</param>
  73. /// <param name="state">
  74. /// The context object of the work item. Used for passing arguments to the work item.
  75. /// </param>
  76. /// <returns>Returns a work item</returns>
  77. public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo,
  78. WorkItemCallback callback, object state)
  79. {
  80. ValidateCallback(callback);
  81. WorkItemInfo workItemInfo = new WorkItemInfo()
  82. {
  83. UseCallerCallContext = wigStartInfo.UseCallerCallContext,
  84. PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback,
  85. CallToPostExecute = wigStartInfo.CallToPostExecute,
  86. DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects,
  87. };
  88. WorkItem workItem = new WorkItem(
  89. workItemsGroup,
  90. workItemInfo,
  91. callback,
  92. state);
  93. return workItem;
  94. }
  95. /// <summary>
  96. /// Create a new work item
  97. /// </summary>
  98. /// <param name="workItemsGroup">The work items group</param>
  99. /// <param name="wigStartInfo">Work item group start information</param>
  100. /// <param name="workItemInfo">Work item information</param>
  101. /// <param name="callback">A callback to execute</param>
  102. /// <param name="state">
  103. /// The context object of the work item. Used for passing arguments to the work item.
  104. /// </param>
  105. /// <returns>Returns a work item</returns>
  106. public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemInfo workItemInfo,
  107. WorkItemCallback callback, object state)
  108. {
  109. ValidateCallback(callback);
  110. ValidateCallback(workItemInfo.PostExecuteWorkItemCallback);
  111. WorkItem workItem = new WorkItem(
  112. workItemsGroup,
  113. new WorkItemInfo(workItemInfo),
  114. callback,
  115. state);
  116. return workItem;
  117. }
  118. /// <summary>
  119. /// Create a new work item
  120. /// </summary>
  121. /// <param name="workItemsGroup">The work items group</param>
  122. /// <param name="wigStartInfo">Work item group start 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. /// <param name="postExecuteWorkItemCallback">
  128. /// A delegate to call after the callback completion
  129. /// </param>
  130. /// <returns>Returns a work item</returns>
  131. public static WorkItem CreateWorkItem(
  132. IWorkItemsGroup workItemsGroup,
  133. WIGStartInfo wigStartInfo,
  134. WorkItemCallback callback,
  135. object state,
  136. PostExecuteWorkItemCallback postExecuteWorkItemCallback)
  137. {
  138. ValidateCallback(callback);
  139. ValidateCallback(postExecuteWorkItemCallback);
  140. WorkItemInfo workItemInfo = new WorkItemInfo();
  141. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  142. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  143. workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute;
  144. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  145. WorkItem workItem = new WorkItem(
  146. workItemsGroup,
  147. workItemInfo,
  148. callback,
  149. state);
  150. return workItem;
  151. }
  152. /// <summary>
  153. /// Create a new work item
  154. /// </summary>
  155. /// <param name="workItemsGroup">The work items group</param>
  156. /// <param name="wigStartInfo">Work item group start information</param>
  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. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  165. /// <returns>Returns a work item</returns>
  166. public static WorkItem CreateWorkItem(
  167. IWorkItemsGroup workItemsGroup,
  168. WIGStartInfo wigStartInfo,
  169. WorkItemCallback callback,
  170. object state,
  171. PostExecuteWorkItemCallback postExecuteWorkItemCallback,
  172. CallToPostExecute callToPostExecute)
  173. {
  174. ValidateCallback(callback);
  175. ValidateCallback(postExecuteWorkItemCallback);
  176. WorkItemInfo workItemInfo = new WorkItemInfo();
  177. workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext;
  178. workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback;
  179. workItemInfo.CallToPostExecute = callToPostExecute;
  180. workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects;
  181. WorkItem workItem = new WorkItem(
  182. workItemsGroup,
  183. workItemInfo,
  184. callback,
  185. state);
  186. return workItem;
  187. }
  188. private static void ValidateCallback(Delegate callback)
  189. {
  190. if (callback != null && callback.GetInvocationList().Length > 1)
  191. {
  192. throw new NotSupportedException("SmartThreadPool doesn't support delegates chains");
  193. }
  194. }
  195. }
  196. #endregion
  197. }