PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Trident/Product/ExecutionService/WFServiceLibrary/SchedulerLogic/TriggerCollection.cs

#
C# | 355 lines | 182 code | 43 blank | 130 comment | 15 complexity | dbb0bcc33a3da6b500e80b4e0cdc116f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. //*********************************************************
  2. //
  3. // Copyright (c) Microsoft. All rights reserved.
  4. // This code is licensed under the Apache License, Version 2.0.
  5. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  6. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  7. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  8. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  9. //
  10. //*********************************************************
  11. namespace Microsoft.Research.eResearch.WFServiceLibrary.SchedulerLogic
  12. {
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Runtime.InteropServices;
  16. using V1 = TaskScheduler.V1;
  17. using V2 = TaskScheduler;
  18. /// <summary>
  19. /// Collection of triggers.
  20. /// </summary>
  21. public sealed class TriggerCollection : IEnumerable<Trigger>, IDisposable
  22. {
  23. private V1.ITask v1TaskDefinition = null;
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. private V2.ITaskDefinition v2TaskDefinition = null;
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. private V2.ITriggerCollection triggerCollection = null;
  32. /// <summary>
  33. /// Gets the number of triggers in the collection.
  34. /// </summary>
  35. public int Count
  36. {
  37. get
  38. {
  39. int triggerCount = 0;
  40. if (this.triggerCollection != null)
  41. {
  42. triggerCount = this.triggerCollection.Count;
  43. }
  44. return triggerCount;
  45. }
  46. }
  47. internal TriggerCollection(V1.ITask iTask)
  48. {
  49. v1TaskDefinition = iTask;
  50. }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="TriggerCollection"/> class.
  53. /// </summary>
  54. /// <param name="iTaskDef">The i task def.</param>
  55. internal TriggerCollection(V2.ITaskDefinition iTaskDef)
  56. {
  57. this.v2TaskDefinition = iTaskDef;
  58. this.triggerCollection = this.v2TaskDefinition.Triggers;
  59. }
  60. /// <summary>
  61. /// Add an unbound <see cref="Trigger"/> to the task.
  62. /// </summary>
  63. /// <param name="unboundTrigger"><see cref="Trigger"/> derivative to add to the task.</param>
  64. /// <returns>Bound trigger.</returns>
  65. public Trigger Add(Trigger unboundTrigger)
  66. {
  67. if (this.v2TaskDefinition != null)
  68. {
  69. unboundTrigger.Bind(this.v2TaskDefinition);
  70. }
  71. else
  72. {
  73. unboundTrigger.Bind(v1TaskDefinition);
  74. }
  75. return unboundTrigger;
  76. }
  77. internal void Bind()
  78. {
  79. foreach (Trigger t in this)
  80. t.SetV1TriggerData();
  81. }
  82. /// <summary>
  83. /// Releases all resources used by this class.
  84. /// </summary>
  85. public void Dispose()
  86. {
  87. if (this.triggerCollection != null)
  88. {
  89. Marshal.ReleaseComObject(this.triggerCollection);
  90. }
  91. this.v2TaskDefinition = null;
  92. v1TaskDefinition = null;
  93. }
  94. /// <summary>
  95. /// Clears all triggers from the task.
  96. /// </summary>
  97. public void Clear()
  98. {
  99. if (this.triggerCollection != null)
  100. {
  101. this.triggerCollection.Clear();
  102. }
  103. else
  104. {
  105. for (int i = this.Count - 1; i >= 0; i--)
  106. {
  107. this.RemoveAt(i);
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Removes the trigger at a specified index.
  113. /// </summary>
  114. /// <param name="index">Index of trigger to remove.</param>
  115. /// <exception cref="ArgumentOutOfRangeException">Index out of range.</exception>
  116. public void RemoveAt(int index)
  117. {
  118. if (index >= this.Count)
  119. {
  120. throw new ArgumentOutOfRangeException("index", index, "Failed to remove Trigger. Index out of range.");
  121. }
  122. if (this.triggerCollection != null)
  123. {
  124. this.triggerCollection.Remove(++index);
  125. }
  126. else
  127. {
  128. v1TaskDefinition.DeleteTrigger((ushort)index); //Remove the trigger from the Task Scheduler
  129. }
  130. }
  131. /// <summary>
  132. /// Gets the collection enumerator for this collection.
  133. /// </summary>
  134. /// <returns>The <see cref="IEnumerator{T}"/> for this collection.</returns>
  135. public IEnumerator<Trigger> GetEnumerator()
  136. {
  137. IEnumerator<Trigger> taskTriggerEnum;
  138. if (v1TaskDefinition != null)
  139. {
  140. taskTriggerEnum = new V1TaskTriggerEnumerator(this.v1TaskDefinition);
  141. }
  142. else
  143. {
  144. taskTriggerEnum = new V2TaskTriggerEnumerator(this.triggerCollection);
  145. }
  146. return taskTriggerEnum;
  147. }
  148. #region IEnumerable<Trigger> Members
  149. #endregion
  150. #region IEnumerable Members
  151. /// <summary>
  152. /// Returns an enumerator that iterates through a collection.
  153. /// </summary>
  154. /// <returns>
  155. /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
  156. /// </returns>
  157. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  158. {
  159. return this.GetEnumerator();
  160. }
  161. internal sealed class V1TaskTriggerEnumerator : IEnumerator<Trigger>
  162. {
  163. private V1.ITask iTask;
  164. private short curItem = -1;
  165. /// <summary>
  166. /// Initializes a new instance of the <see cref="V1TaskTriggerEnumerator"/> class.
  167. /// </summary>
  168. /// <param name="task">The task.</param>
  169. internal V1TaskTriggerEnumerator(V1.ITask task)
  170. {
  171. this.iTask = task;
  172. }
  173. /// <summary>
  174. /// Gets the element in the collection at the current position of the enumerator.
  175. /// </summary>
  176. /// <value></value>
  177. /// <returns>
  178. /// The element in the collection at the current position of the enumerator.
  179. /// </returns>
  180. public Trigger Current
  181. {
  182. get
  183. {
  184. return Trigger.CreateTrigger(this.iTask.GetTrigger((ushort)curItem));
  185. }
  186. }
  187. /// <summary>
  188. /// Releases all resources used by this class.
  189. /// </summary>
  190. public void Dispose()
  191. {
  192. this.iTask = null;
  193. }
  194. /// <summary>
  195. /// Gets the element in the collection at the current position of the enumerator.
  196. /// </summary>
  197. /// <value></value>
  198. /// <returns>
  199. /// The element in the collection at the current position of the enumerator.
  200. /// </returns>
  201. object System.Collections.IEnumerator.Current
  202. {
  203. get { return this.Current; }
  204. }
  205. /// <summary>
  206. /// Advances the enumerator to the next element of the collection.
  207. /// </summary>
  208. /// <returns>
  209. /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
  210. /// </returns>
  211. /// <exception cref="T:System.InvalidOperationException">
  212. /// The collection was modified after the enumerator was created.
  213. /// </exception>
  214. public bool MoveNext()
  215. {
  216. if (++this.curItem >= iTask.GetTriggerCount())
  217. return false;
  218. return true;
  219. }
  220. /// <summary>
  221. /// Sets the enumerator to its initial position, which is before the first element in the collection.
  222. /// </summary>
  223. /// <exception cref="T:System.InvalidOperationException">
  224. /// The collection was modified after the enumerator was created.
  225. /// </exception>
  226. public void Reset()
  227. {
  228. this.curItem = -1;
  229. }
  230. }
  231. /// <summary>
  232. /// Supports a simple iteration over task trigger.
  233. /// </summary>
  234. internal sealed class V2TaskTriggerEnumerator : IEnumerator<Trigger>
  235. {
  236. /// <summary>
  237. /// Newer version enumeration.
  238. /// </summary>
  239. private System.Collections.IEnumerator iEnum;
  240. /// <summary>
  241. /// Initializes a new instance of the <see cref="V2TaskTriggerEnumerator"/> class.
  242. /// </summary>
  243. /// <param name="iCollection">The i collection.</param>
  244. internal V2TaskTriggerEnumerator(V2.ITriggerCollection iCollection)
  245. {
  246. this.iEnum = iCollection.GetEnumerator();
  247. }
  248. #region IEnumerator<Trigger> Members
  249. /// <summary>
  250. /// Gets the element in the collection at the current position of the enumerator.
  251. /// </summary>
  252. /// <value></value>
  253. /// <returns>
  254. /// The element in the collection at the current position of the enumerator.
  255. /// </returns>
  256. public Trigger Current
  257. {
  258. get
  259. {
  260. return Trigger.CreateTrigger((V2.ITrigger)this.iEnum.Current);
  261. }
  262. }
  263. #endregion
  264. #region IDisposable Members
  265. /// <summary>
  266. /// Releases all resources used by this class.
  267. /// </summary>
  268. public void Dispose()
  269. {
  270. this.iEnum = null;
  271. }
  272. #endregion
  273. #region IEnumerator Members
  274. /// <summary>
  275. /// Gets the element in the collection at the current position of the enumerator.
  276. /// </summary>
  277. /// <value></value>
  278. /// <returns>
  279. /// The element in the collection at the current position of the enumerator.
  280. /// </returns>
  281. object System.Collections.IEnumerator.Current
  282. {
  283. get { return this.Current; }
  284. }
  285. /// <summary>
  286. /// Advances the enumerator to the next element of the collection.
  287. /// </summary>
  288. /// <returns>
  289. /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
  290. /// </returns>
  291. /// <exception cref="T:System.InvalidOperationException">
  292. /// The collection was modified after the enumerator was created.
  293. /// </exception>
  294. public bool MoveNext()
  295. {
  296. return this.iEnum.MoveNext();
  297. }
  298. /// <summary>
  299. /// Sets the enumerator to its initial position, which is before the first element in the collection.
  300. /// </summary>
  301. /// <exception cref="T:System.InvalidOperationException">
  302. /// The collection was modified after the enumerator was created.
  303. /// </exception>
  304. public void Reset()
  305. {
  306. this.iEnum.Reset();
  307. }
  308. #endregion
  309. }
  310. #endregion
  311. }
  312. }