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);
                }
                int delta = Convert.ToInt32((x.Begin - DateTime.Now).TotalMilliseconds);
                if (delta < 0)
                {
                    delta = delta % x.Interval;
                    if (delta < 0)
                    {
                        delta += x.Interval;
                    }
                }
                Task.Factory.StartNew(() =>
                {
                    var timer = new Timer(t => {
                        Execute(x.Id);
                    }, null, delta, x.Interval);
                    JobTimers.Add(x.Id, timer);
                });
            }
        }
        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();
        }
Example #3
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 > Int32.MaxValue; delta = delta - Int32.MaxValue)
                        {
                            await Task.Delay(Int32.MaxValue);
                        }
                    }
                    var timer = new Timer(t => {
                        Execute(x.Id);
                    }, null, Convert.ToInt32(delta), x.Interval);
                    JobTimers.Add(x.Id, timer);
                });
            }
        }