You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Euro.Transfer.Base
  7. {
  8. /// <summary>
  9. /// 工作项
  10. /// </summary>
  11. public abstract class ServiceTask : ServiceBase
  12. {
  13. //配置对象
  14. private ServiceConfig mConfigObject;
  15. //下次运行时间
  16. private DateTime mNextTime;
  17. //任务是否在运行中
  18. protected bool mIsRunning;
  19. //初始化客戶端
  20. public static HubTransfer hubClient = new HubTransfer();
  21. /// <summary>
  22. /// 构造函数
  23. /// </summary>
  24. public ServiceTask()
  25. {
  26. //变量初始化
  27. this.mNextTime = DateTime.Now;
  28. this.mIsRunning = false;
  29. }
  30. /// <summary>
  31. /// 配置对象
  32. /// </summary>
  33. public ServiceConfig ConfigObject
  34. {
  35. get { return this.mConfigObject; }
  36. set { this.mConfigObject = value; }
  37. }
  38. /// <summary>
  39. /// 开始工作
  40. /// </summary>
  41. public void StartJob()
  42. {
  43. if (this.mConfigObject != null && this.mNextTime != null)
  44. {
  45. if (this.mConfigObject.Enabled.ToLower() == "true")
  46. {
  47. if (DateTime.Now >= this.mNextTime)
  48. {
  49. if (!this.mIsRunning)
  50. {
  51. this.mNextTime = DateTime.Now.AddSeconds((double)this.mConfigObject.Interval);
  52. this.Start();
  53. }
  54. }
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// 停止工作
  60. /// </summary>
  61. public void StopJob()
  62. {
  63. this.mConfigObject = null;
  64. this.mNextTime = DateTime.Now;
  65. this.mIsRunning = false;
  66. this.Stop();
  67. }
  68. #region 子类必需实现的抽象成员
  69. /// <summary>
  70. /// 开始工作
  71. /// </summary>
  72. protected abstract void Start();
  73. /// <summary>
  74. /// 停止工作
  75. /// </summary>
  76. protected abstract void Stop();
  77. #endregion
  78. }
  79. }