Click here to Skip to main content
12,068,967 members (66,039 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET threads
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 18-Aug-11 21:43pm
Edited 18-Aug-11 21:57pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
sangita navale 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 19 Aug 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100