|
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections.Concurrent;
-
- namespace BPASmartClient.Helper
- {
- /// <summary>
- /// 延时上升沿触发
- /// </summary>
- public class DelayRTrig
- {
-
- private volatile static ConcurrentDictionary<string, DelayRTrig> _Instance;
- public static DelayRTrig GetInstance(string name)
- {
- if (_Instance == null) _Instance = new ConcurrentDictionary<string, DelayRTrig>();
- if (!_Instance.ContainsKey(name)) _Instance.TryAdd(name, new DelayRTrig());
- return _Instance[name];
- }
-
- private DelayRTrig() { }
-
- /// <summary>
- /// 计时条件
- /// </summary>
- //public bool IN { get; set; }
-
- /// <summary>
- /// 当前时间
- /// </summary>
- public long ET { get; private set; } = 0;
-
- private bool flag1;
- public bool Q { get; private set; }
- private bool IN1
- {
- set
- {
- Q = value && !flag1;
- flag1 = value;
- }
- }
-
- private bool flag;
- DateTime startTime = new DateTime();
-
- /// <summary>
- /// 开始计时(时间单位/秒)
- /// </summary>
- /// <param name="PT">计时时间(单位 秒)</param>
- /// <returns></returns>
- public bool Start(bool IN, int PT)
- {
- if (IN)
- {
- if (!flag)
- {
- startTime = DateTime.Now;
- flag = true;
- }
- if (ET < PT) ET = Convert.ToInt64(DateTime.Now.Subtract(startTime).TotalSeconds);
- }
- else
- {
- ET = 0;
- flag = false;
- }
- IN1 = ET >= PT;
- return Q;
- }
- }
- }
|