3

I need to implement a function that contains a loop of instructions that should run every .01 second is there a way to implement a loop(e.g for each time step) that can do that thanks in advance

rochb
  • 2,249
  • 18
  • 26
Mohamed
  • 1,537
  • 2
  • 10
  • 14
  • How precise do you need that to be? For example, suppose it runs 64 times a second instead of 100, is that acceptable? Suppose it runs 10003 times in 100 seconds, is that acceptable? Timer granularity might not be as precise as you need it to be; Windows is not a realtime operating system. You need to use special techniques if you need high precision timers. – Eric Lippert Oct 20 '10 at 23:45
  • please mark your question as answered – cpoDesign Apr 09 '13 at 08:12

2 Answers2

8

You can use Timer class like this:

Timer t = new Timer(100);
t.Elapsed += (sender, e) =>
    {
        for (int i = 0; i < 10; i++)
        {
            // your loop
        }
    };

t.Start();
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
as-cii
  • 12,819
  • 4
  • 41
  • 43
4

You can generate a recurring event using the Timer class.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501