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.

DelayTTrig.cs 1.7 KiB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections.Concurrent;
  5. namespace HBLConsole.Service
  6. {
  7. /// <summary>
  8. /// 延时下降沿触发
  9. /// </summary>
  10. public class DelayTTrig
  11. {
  12. private volatile static ConcurrentDictionary<string, DelayTTrig> _Instance;
  13. public static DelayTTrig GetInstance(string name)
  14. {
  15. if (_Instance == null) _Instance = new ConcurrentDictionary<string, DelayTTrig>();
  16. if (!_Instance.ContainsKey(name)) _Instance.TryAdd(name, new DelayTTrig());
  17. return _Instance[name];
  18. }
  19. private DelayTTrig() { }
  20. /// <summary>
  21. /// 当前时间
  22. /// </summary>
  23. public long ET { get; private set; } = 0;
  24. private bool flag1;
  25. private bool Q { get; set; }
  26. private bool IN1
  27. {
  28. set
  29. {
  30. Q = !value && flag1;
  31. flag1 = value;
  32. }
  33. }
  34. private bool flag;
  35. DateTime startTime = new DateTime();
  36. /// <summary>
  37. /// 开始计时(时间单位/秒)
  38. /// </summary>
  39. /// <param name="PT">计时时间(单位 秒)</param>
  40. /// <returns></returns>
  41. public bool Start(bool IN, int PT)
  42. {
  43. if (IN)
  44. {
  45. if (!flag)
  46. {
  47. startTime = DateTime.Now;
  48. flag = true;
  49. }
  50. if (ET < PT) ET = Convert.ToInt64(DateTime.Now.Subtract(startTime).TotalSeconds);
  51. }
  52. else
  53. {
  54. ET = 0;
  55. flag = false;
  56. }
  57. IN1 = ET >= PT;
  58. return Q;
  59. }
  60. }
  61. }