Dargento
Dargento

Reputation: 43

I cannot reach 1ms accuracy with .net Timers. How can I execute a function periodically with a Multimedia Timer every 30ms? (With 1 ms accuracy)

In my C# project I want to execute a function periodically every 30ms. First I tried the .net Timers, but I have found out that these timer have no more accuracy then 15ms due to Windows OS restrictions. I read in some stackoverflow entries that the only way in Windows is to use a Multimedia timer. (or you change some system clock settings, what I don't want to do).I have seen some code examples but as I am a very fresh junior programmer they were too complex for me and I even didn't get where to insert my Periodic function code inside.

  1. Is it really true that I can achieve 1ms accuracy with Multimedia timer on a Windows OS?

  2. If yes, can anyone give me a simple example how I can use a Multimedia timer to execute my function periodically? My code is running on a Windows 2019 Server OS.

This is the code I have used till now: (Bad accuracy 15ms)

System.Timers.Timer Periodic_timer = new System.Timers.Timer();
public void InitializeTimer_Periodic_timer()
{            
    Periodic_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent_Periodic_timer);
    Periodic_timer.Interval = 30;
    Periodic_timer.Start();
}

private void OnTimedEvent_Periodic_timer(object sender, EventArgs e)
{
    Periodic_function();
}

Upvotes: 1

Views: 528

Answers (0)

Related Questions