c# - Calling a method at the fastest possible interval (timers) -


i have method want run several thousands of times per second. i'm running @ rate of once every millisecond using dispatchertimer, interval property set 1ms.

now, dispatchertimer isn't fastest, high-precision timer available , since tick event handler running on ui thread it's not going execute more that. code i'm calling doesn't need run on ui thread seems i'm limiting myself no real reason.

what options if want higher frequency? should use system.threading.timer? there specific timer high-frequency, high-precision operations? should forego timers altogether , else?

note although tried (but failed, due lack of reputation) tag windowsiot (because need run there , possibly new devices library has stuff this), applies .net applications in general well, wpf, winforms, winrt, etc...

the underlying problem when yield cpu time, you're doing in os scheduling system. means operations thread.sleep, or indeed, waiting callback of system timer, limited frequency of system timer. that's 15.6ms - far longer 1ms. way wpf gets around this, allow smooth animations etc., while animation proceeding, change global timer frequency. minimum 0.5ms might enough - provided rest of code can execute in 0.5ms (for total of 1ms "frame time"). obviously, it's bit tricky have reliable frequency.

so boils down why you're trying run 1000 times per second, , what you're doing in cycle.

and you're guaranteed not kind of resolution. if set global timer 0.5ms, , make timer executes every second, you'll still fighting other threads trying execute.

one of approaches used in gaming using high-resolution timer - queryperformancecounter, used internally in stopwatch. however, doesn't allow yield - have actively spin (unless can thread.sleep long enough allow use - see above). , note games use fixed step timing - scenario limited 60 fps - 17ms per cycle. that's far cry 1ms per cycle. when not using fixed step timing, can make lot of cool stuff, have account actual length of time step - not entirely simple, , possibly not applicable case.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -