Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Threading
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);         
 
<pre>
        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" &amp;&amp; EFrequency == "Event" || EType == "Stop" &amp;&amp; EFrequency == "Event" || EType == "Event" &amp;&amp; EFrequency == "Event")
        {
            int freq = 500;
            tsDueTime = new TimeSpan(0, 0, 0, 0, freq);
            tsPeriod = new TimeSpan(0, 0, 0, 0, freq);
        }
 
    }</pre>

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 at 19-Aug-11 3:08am
   
see my code any suggestion to change the code
Dalek Dave at 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
0 OriginalGriff 480
1 Sergey Alexandrovich Kryukov 445
2 Maciej Los 290
3 ProgramFOX 275
4 CHill60 225
0 OriginalGriff 550
1 Sergey Alexandrovich Kryukov 470
2 Maciej Los 310
3 ProgramFOX 275
4 Peter Leow 250


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 19 Aug 2011
Copyright © CodeProject, 1999-2015
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