Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        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);
                });
            }
        }