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
Asked
Active
Viewed 5,421 times
3
-
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 Answers
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
-
1+1 for the example, although I also gave Robert +1 for having the answer first. – Steven Sudit Oct 20 '10 at 22:08
-
-
-
-
Is the timer granularity really that good? It might be instructive to test this and see how many times per second you are actually getting called. – Eric Lippert Oct 20 '10 at 23:43