4
Answers

Performance of the system.threading.timers in windows service

vrushali katkade

vrushali katkade

13y
5.1k
1
I have developed the windows service in C#.NET . In this, I have used system.threading.timer .
when i have start the 10 second interval timer there is some variation between the execution timing.
//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);  
      }
public void databaseTrensfer()
{
//code
}
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();
           }
       }
i have pass the frequency to set the timer & start the number of timer simultaneously
my o/p
2011-08-22 13:42:08.0
2011-08-22 13:42:18.0
2011-08-22 13:42:28.0
2011-08-22 13:42:38.15
.
.
.
2011-08-22 14:28:38.984
2011-08-22 14:28:48.984
2011-08-22 14:28:59.15
2011-08-22 14:29:09.15
2011-08-22 14:29:19.15
.
.
2011-08-22 15:14:59.953
2011-08-22 15:15:09.968
2011-08-22 15:15:19.968
i got this o/p timer started at the 08 seconds of variation  then shifting to the 09 second
why all timers are not executing in exact timing? 
thanks in advance


Answers (4)