SequenceExecutor.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Flyinsono.Client.Test.Utilities.Executors
  4. {
  5. /// <summary>
  6. /// This executor will run workers one by one.
  7. /// </summary>
  8. /// <typeparam name="T"></typeparam>
  9. public class SequenceExecutor<T>:ExecutorBase<T> where T: class
  10. {
  11. private readonly Queue<ExecuteWorker<T>> _workers = new Queue<ExecuteWorker<T>>();
  12. public SequenceExecutor(string name) : base(name)
  13. {
  14. }
  15. public SequenceExecutor(string name, TimeSpan executeDelayTime) : base(name, executeDelayTime)
  16. {
  17. }
  18. /// <summary>
  19. /// Gets current worker count in queue.
  20. /// </summary>
  21. /// <returns></returns>
  22. protected override int GetWorkerCount()
  23. {
  24. return _workers.Count;
  25. }
  26. /// <summary>
  27. /// Add one worker into the queue.
  28. /// </summary>
  29. /// <param name="worker"></param>
  30. protected override void AddWorker(ExecuteWorker<T> worker)
  31. {
  32. _workers.Enqueue(worker);
  33. }
  34. /// <summary>
  35. /// Take one worker from the queue.
  36. /// </summary>
  37. /// <returns></returns>
  38. protected override ExecuteWorker<T> TakeWorker()
  39. {
  40. return _workers.Dequeue();
  41. }
  42. /// <summary>
  43. /// Gets all workers.
  44. /// </summary>
  45. /// <returns></returns>
  46. protected override IEnumerable<ExecuteWorker<T>> GetWorkers()
  47. {
  48. return _workers.ToArray();
  49. }
  50. /// <summary>
  51. /// Clear all workers which not stated.
  52. /// </summary>
  53. protected override void CancelWorkers()
  54. {
  55. _workers.Clear();
  56. }
  57. /// <summary>
  58. /// Get the rest not working workers
  59. /// </summary>
  60. /// <returns></returns>
  61. public IEnumerable<ExecuteWorker<T>> GetRestWorkers()
  62. {
  63. return GetWorkers();
  64. }
  65. }
  66. }