简体   繁体   English

Windows Service中的System.Threading.Timer间隔

[英]System.Threading.Timer interval in Windows Service

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 eg I have set the timer to the 10 sec then it executed at every 10 sec but sometimes it will started at 11 sec . 我开发了Windows服务,并在寡妇服务中使用了system.threading.timer。在这种情况下,计时器工作正常,但有时会改变执行时间,例如,我将计时器设置为10秒,然后每10秒执行一次秒,但有时它将在11秒开始。 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

i have used the system.threading.timer 我用过system.threading.timer

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 == "Millisecond")
            {
                int frquency1 = Convert.ToInt32(row["Tfrquency"]);
                tsDueTime = new TimeSpan(0, 0, 0, 0, frquency1);//frquency1=interval timing
                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();
            }      

        }

thanks in advance 提前致谢

I'd be extremely wary about needing such exact intervals between activities. 对于活动之间需要如此精确的间隔,我会非常警惕。 If you do need such accuracy, you'd usually have to set a timer with a much smaller interval, and then check whether the correct time has been reached to perform the next action. 如果确实需要这种精度,通常必须设置一个更小的间隔时间的计时器,然后检查是否已达到执行下一个动作的正确时间。

Eg if you want 10 second intervals, and want to avoid 11 second intervals, you would set the timer interval at something like 0.1 seconds, and then just do nothing if less than 10 seconds have passed since you last did something. 例如,如果您想要10秒间隔,而又想避免11秒间隔,则可以将计时器间隔设置为0.1秒,然后,如果自您上次执行操作以来不到10秒,则不执行任何操作。

Alternatively, you might leave the interval at 10 seconds generally, but measure how frequently the timer is elapsing and alter the interval to steer the average interval back to 10 seconds - If you've just had a few intervals of 11 seconds, alter the timer to be 9 (or 9.5) seconds until the average returns back to 10 seconds. 另外,您通常可以将间隔设置为10秒,但要测量计时器流逝的频率,并更改间隔以将平均间隔控制回10秒-如果您只有11秒的间隔,请更改计时器变为9(或9.5)秒,直到平均值恢复为10秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM