3

I need code that will provide "Ticks" of 10 miliseconds exactly and I am buffled in view of the many articles some contradicting others regarding how to accomplish this in Windows.

I've managed the first step - using the MultiMedia timer to provide sleeps of 1 mili.

Is there a way to ensure that the sleep code will work on all machines IsHighResolution either true or false, single or multi processor systems etc?

[edit] A previous question ragarding the problem with using StopWatch in a multicore system: Multicore and thread aware .Net stopwatch? [/edit]

Community
  • 1
  • 1
Joezer
  • 625
  • 1
  • 9
  • 20
  • Do you want to measure these ticks or do you want to call code in these intervals? For the first, the very best solution is to use the Stopwatch – Georg Apr 20 '15 at 07:38
  • 1
    @Georg - to call a method on exact intervals. Also, you seem to be unaware of the problem with StopWatch, have a look at the link I added in the question. – Joezer Apr 20 '15 at 07:39
  • 2
    @Maimonides Those problems tend not to occur on recent processors (i.e. in the last 5 years) but in any case can be fixed by setting thread affinity. But it won't let you sleep for an exact number of milliseconds. – Matthew Watson Apr 20 '15 at 08:05
  • @MatthewWatson - Good news about the new processors! but, how than can I achieve a shortspan sleep? I have computation cycles that upon conclusion I need to sleep the remaining time till the next cycle. I have considered *busy waiting* but I'd prefer another method if possible. – Joezer Apr 20 '15 at 08:08
  • @Maimonides Because Windows is not a real time operating system, there is actually no way you can guarantee millisecond precision in waits. You might be able to do it most of the time, but it may occasionally fail. – Matthew Watson Apr 20 '15 at 08:28

1 Answers1

0

sorry that I can't write this in comments... If you have the possibility to use a second thread you could use a loop to waste the time...like this:

    class yourTimer{ 
    public  void startTimer(Action a)
    {
        ParameterizedThreadStart fu = new ParameterizedThreadStart(waiter);
        Thread t1 = new Thread(fu);
        t1.Start(a);
    }
    public void stopTimer()
    private void waiter(object o)
    {
        Action ac = (Action)o;
        while(waitin)
        {
            if(DateTime.Now.Millisecond%10==0)
            {
                ac.Invoke();
            }
            continue;
        }
    }
  }

but that's a quite ugly solution I think ...

leAthlon
  • 734
  • 1
  • 10
  • 22