本文介绍了Windows Service中的System.Threading.Timer间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了Windows服务&在寡妇服务中使用system.threading.timer.在这种情况下,计时器可以正常工作,但有时会改变执行时间,例如我将计时器设置为10秒,然后每10秒执行一次,但有时它将在11秒开始.我有这个输出

I have developed the windows service & in widows service used the system.threading.timer .in this the timers works fine but sometimes it will shifting the timing of the execution e.g. I have set the timer to the 10 sec then it executed at every 10 sec but sometimes it will started at 11 sec . I have got this output

18-08-2011 10:00:10
18-08-2011 10:00:10
18-08-2011 10:00:20
18-08-2011 10:00:30
18-08-2011 10:00:40
18-08-2011 10:00:11
18-08-2011 10:00:22
18-08-2011 10:00:33
18-08-2011 10:00:44


但我希望输出为


but i want the output as

18-08-2011 10:00:10
18-08-2011 10:00:20
18-08-2011 10:00:30
18-08-2011 10:00:40
18-08-2011 10:00:50
18-08-2011 10:00:00
18-08-2011 10:00:10
18-08-2011 10:00:20


//代码


//code

  public void SetTimers(int timer, DataRow row)
        {
            TimeSpan dueTime;
            TimeSpan interval;
            SetTimeIntervals(row, out dueTime, out interval);

            timer1[timer] = new System.Threading.Timer(databaseTrensfer, row, dueTime, interval);

        }



private void SetTimeIntervals(DataRow row, out TimeSpan tsDueTime, out TimeSpan tsPeriod)
        {

            string alarmType = Convert.ToString(row["EBase"]);
            string EType = Convert.ToString(row["EType"]);
            string EFrequency = Convert.ToString(row["EFrequncy"]);
            if (alarmType == "Milisecond")
            {
                int frquency1 = Convert.ToInt32(row["Tfrquency"]);
                tsDueTime = new TimeSpan(0, 0, 0, 0, frquency1);//frquency1=interval
                tsPeriod = new TimeSpan(0, 0, 0, 0, frquency1);
            }
            else if (alarmType == "Second")
            {
                int frquency1 = Convert.ToInt32(row["Tfrquency"]);
                tsDueTime = new TimeSpan(0, 0, 0, frquency1);
                tsPeriod = new TimeSpan(0, 0, 0, frquency1);
            }
            else if (alarmType == "Once")
            {
                tsDueTime = new TimeSpan(0, 0, 0);
                tsPeriod = new TimeSpan(0, 0, 0);
            }
            else if (alarmType == "Minute")
            {

                int frquency1 = Convert.ToInt32(row["Tfrquency"]);
                tsDueTime = new TimeSpan(0, frquency1, 0);
                tsPeriod = new TimeSpan(0, frquency1, 0);
            }
            else if (alarmType == "Hour")
            {

                int minute = 0;
                int frquency1 = 1;
                if (Convert.ToString(row["RelativeFactor"]) != "")
                    minute = Convert.ToInt32(row["RelativeFactor"]);
                if (Convert.ToString(row["Tfrquency"]) != "")
                    frquency1 = Convert.ToInt32(row["Tfrquency"]);

                tsDueTime = new TimeSpan(frquency1, minute, 0);
                tsPeriod = new TimeSpan(frquency1, 0, 0);
            }
            else
            {
                tsDueTime = new TimeSpan();
                tsPeriod = new TimeSpan();
            }

            if (EType == "Start" && EFrequency == "Event" || EType == "Stop" && EFrequency == "Event" || EType == "Event" && EFrequency == "Event")
            {
                int freq = 500;
                tsDueTime = new TimeSpan(0, 0, 0, 0, freq);
                tsPeriod = new TimeSpan(0, 0, 0, 0, freq);
            }

        }


在此先感谢


thanks in advance

推荐答案


这篇关于Windows Service中的System.Threading.Timer间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:41