I have a function that should send a signal after an specific interval, accurate to 1 millisecond (ms).
But it seems like my timer needs slightly longer than he should, i.e. I pass the function a TimeSpan with 20ms but the timer needs 30ms per tick. I wrote now a timer myself with a Stopwatch
but I'm still wondering why the timer needs more time to execute ?
private System.Timers.Timer timer;
private void startResetTimer(TimeSpan time)
{
timer = new System.Timers.Timer(time.TotalMilliseconds);
timer.Elapsed += OnTimedEvent;
timer.AutoReset = true;
timer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
if (!waitForTimerReset.Set())
Console.WriteLine("Could not wake up any threads!");
}
In my Code the only thing the timer executes is the waitForTimerReset.Set()
Method which allows Threads to continue after they got stopped by a ManualResetEvent
, which means that this call should not take 10 ms.