public void RestartDynamicTimers() { var jobs = DynamicJobs.GetJobs(); foreach (var x in jobs) { if (JobTimers.ContainsKey(x.Id)) { JobTimers[x.Id].Dispose(); JobStatus[x.Id] = false; JobTimers.Remove(x.Id); JobStatus.Remove(x.Id); } } StartDynamicTimers(); }
private void StartDynamicTimers() { var jobs = DynamicJobs.GetJobs(); foreach (var x in jobs) { // 如果Hard Timer已经启动则注销实例 if (JobTimers.ContainsKey(x.Id)) { JobTimers[x.Id].Dispose(); JobStatus[x.Id] = false; JobTimers.Remove(x.Id); JobStatus.Remove(x.Id); } long delta = Convert.ToInt64((x.Begin - DateTime.Now).TotalMilliseconds); if (delta < 0) { delta = delta % Convert.ToInt64(x.Interval); if (delta < 0) { delta += Convert.ToInt64(x.Interval); } } Task.Factory.StartNew(async() => { if (delta > int.MaxValue) { for (; delta > int.MaxValue; delta = delta - int.MaxValue) { await Task.Delay(int.MaxValue); } } var timer = new Timer(t => { Execute(x.Id); }, null, Convert.ToInt32(delta), x.Interval); JobTimers.Add(x.Id, timer); }); } }