Yeah, but I need to use QueryPerformanceCounter for accuracy. Sleep/Timer/GetTickCout/everything else only "refreshes" 64 times a second. I need to be in the microseconds/milliseconds.
This code here uses Sleep if necessary and makes sure the code is executed the required amount in seconds. You can actually make the code execute more often. It isn't perfectly accurate (thanks to Sleep), but a human can't possibly notice the millisecond differences. I can't really see anyone using this for thousands of iterations/second.
Attached a sample project which has one iteration (0) doing nothing for 1000 times/second, and another (1) for updating Form1.Caption every one seconds.
So basically this is just a very advanced version of the sample code by CVMichael (using QueryPerformanceCounter instead and controls the timings).
Add another tick that does nothing; it is the downside of it, you must have one high frequency tick to keep things responsive. Don't worry, it only calls DoEvents when there is a need for it
30 - 50 ticks or so should be very much enough (to fool human to think the program is fully responsive). At only 5 - 10 ticks Windows starts to think the program is unresponsive.