precise timing on Windows

I’m trying to convert an oF app from Mac to PC that needs precise timing. The timer code is running in audioRequested, which on the Mac is running in nice 3ms intervals, but on Windows the timing is all over the place.

I’ve tried the optional ‘precise timing’ flag (forgot the exact name now) but that resulted in a whole bunch of compile errors here (code::blocks on PC)

I’ve read a few articles on using Multimedia timers but couldn’t get them compiled, compiler didn’t like the inline asm…

I’ve also tried using a thread with a sleeping timer but even that is highly unprecise, I’ve read other threads on the forum about timing and tried a lot but to no avail.

Did someone manage to solve this on Windows? or have any ideas, or an example on how to get a multimedia timer working?

the best thing to do in this case is to run a thread that repeatedly sleep for a short period of time, until you’re ready to run. for example, to sleep 3ms you should set a high performance timer (i think that’s what they’re called on windows) called start to ‘now’ then make a while loop:

// sleep for 3ms  
start = 'now';  
while(true) {   
  sleep 100ns;  
  if ( 'now'-start > 3ms )  


thanks Damian,
I’m not certain you can sleep at such fine intervals though, looking at oF’s timing code it seems that Windows sleeps in milliseconds unlike OSX which sleeps in microseconds.

also, wouldn’t the ‘if ( ‘now’-start > 3ms )’ bit be imprecise because of the granularity of ofGetElapsedMillis()? or is there a more precise method for that?

i’d suggest using ofGetELapsedTimef() – i’m pretty sure it’s internally using nanosecond timers :slight_smile:

otherwise on Windows you can use HighPerformanceTimer

aha, that’s good to know! thanks again

no problem – ps - i think we worked together in amsterdam :wink: nice to see you here!