0

i ve used some ex from stackoverflow but sometimes my timer fire twice: this clock should fire each one minute, but just assume that it fires 00:00:59.666 then step1 = 59; step2 = 59; step3 = 1; return (1000-666); so it should fire not next minute but next 333 mili second, what can i do?

public int SyncTime = 60;
Clock = new System.Timers.Timer {AutoReset = false};
            Clock.Elapsed += DoJob;
            Clock.Interval = GetSync(SyncTime);
            Clock.Start()
   private void DoJob(object sender, ElapsedEventArgs elapsedEventArgs)
   {

            Clock.Interval = GetSync(SyncTime);
            Clock.Start();

    }
    private static double GetSync(int syncTime)
    {
        DateTime now = DateTime.Now;
        int step1 = (now.Minute * 60 + now.Second); //amount of miliseconds in this hour
        int step2 = step1 % syncTime;               //amount of miliseconds since last update
        int step3 = syncTime - step2;               //amount of miliseconds to next upadte
        return (step3 * 1000 - now.Millisecond);
    }
kosnkov
  • 5,609
  • 13
  • 66
  • 107
  • 5
    Your code is incomplete... you appear to have statements in the middle of nowhere, with no semi-colon after one of them. If you could create a short but complete program which demonstrates the problem, you'd be much more likely to get an answer. – Jon Skeet Nov 29 '10 at 23:51
  • Also, what time are you synchronising? And why? – Alastair Pitts Nov 29 '10 at 23:56
  • this is complete, i even gave ex how this fun can return wrong value, i look for answer – kosnkov Nov 29 '10 at 23:58
  • 1
    Your abbreviations are hard to read. What is "ex"? I don't get any "fun" reading your text. – aaaa bbbb Nov 30 '10 at 00:05
  • Jon Skeet was talking about the first line "Clock.Start()" not having the trailing semicolon. Your code posted here is not complete. – aaaa bbbb Nov 30 '10 at 00:08
  • @antonlavey, not exactly, problem is diffrent, when sometimes timer will fire not 60 seconds past but 59,888 (what is possible), then this function will return not 60,112 (so next minute) but 112, what will fire timer again in 112 miliseconds – kosnkov Nov 30 '10 at 08:32
  • Ah I was thinking it wanted to fire at exactly each minute mark. Updated calculation above to correct this issue. – Adrian Nov 30 '10 at 17:37
  • Kind user428547, this is not an answer. This should be moved to be a comment on antonlavey's answer, and this 'answer' deleted. – aaaa bbbb Nov 30 '10 at 21:37

2 Answers2

0

Ok not sure if this is the answer to your specific issue but your GetSync function is calculating the remaining time till the full minute incorrectly.

It should be (unless I am missing something which is completely possible):

private static double GetSync(int syncTime)
{
  DateTime now = DateTime.Now;      
  return ((2 * syncTime) - now.Second) * 1000) - now.Millisecond; 
}

This may resolve your issue as calculating the remaining time this way will never result in you returning a 0 value from GetSync.

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
Adrian
  • 2,911
  • 1
  • 17
  • 24
-1

I suspect that the issue is in the line:

Clock = new System.Timers.Timer {AutoReset = false};

I think this should read:

Clock = new System.Timers.Timer {AutoReset = true};

This will mean that the timer will fire only once until you reissue the Start command.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • 1
    Poster is reissuing the start command in his "DoWork" function. – Adrian Nov 29 '10 at 23:53
  • 1
    noo i like to fire only once, then synchronize time and fire again, AutoReset = false means that timer will fire only once – kosnkov Nov 29 '10 at 23:54