|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Collections.Concurrent;
-
- namespace HBLConsole.Service
- {
- /// <summary>
- /// 延时操作
- /// </summary>
- public class Delay
- {
- /// <summary>
- /// 计时条件
- /// </summary>
- //public bool IN { get; set; }
-
-
- private volatile static ConcurrentDictionary<string, Delay> _Instance;
- public static Delay GetInstance(string name)
- {
- if (_Instance == null) _Instance = new ConcurrentDictionary<string, Delay>();
- if (!_Instance.ContainsKey(name)) _Instance.TryAdd(name, new Delay());
- return _Instance[name];
- }
- private Delay() { }
-
- /// <summary>
- /// 移除指定的定时器
- /// </summary>
- /// <param name="name"></param>
- public static void RemoveDelay(string name)
- {
- if (_Instance.ContainsKey(name))
- _Instance.TryRemove(name, out Delay delay);
- }
-
- /// <summary>
- /// 当前时间
- /// </summary>
- public long ET { get; private set; } = 0;
- public bool Q { get; private set; }
-
- 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;
- }
- Q = ET >= PT;
- return Q;
- }
-
- }
- }
|