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 . 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

  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.

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.

链接地址: http://www.djcxy.com/p/46572.html

上一篇: 使用WCF将SQL Azure连接到Windows Phone 7

下一篇: Windows服务中的System.Threading.Timer时间间隔