Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 18-Aug-11 20:57pm
v2

1 solution

 
Share this answer
 
Comments
vrushali katkade 19-Aug-11 3:08am    
see my code any suggestion to change the code
Dalek Dave 19-Aug-11 3:28am    
Good Link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900