high precision event triggering in c#
-
I have an application in which I require event triggers every 1-5ms or so. I am using C# and as far as I can tell, the usual triggering method in C# actually uses the DateTime.Now.Ticks property to generate event triggers. Thus its resolution is also limited to the resolution of DateTime.Now.Ticks which is 15.625 ms on my m/c (i.e. the smallest time interval whch DateTime.Now.Ticks can measure is 15.625 ms). If I set my trigger interval to anywhere between 1 - 15 ms I actually get triggers only after 15.625 ms. If I set my trigger interval to 16-31 ms I get triggers after every 31.25 ms and so on. I can email you a .cs file that illustrates this point. If you run the program it asks you the trigger interval you want to set. Then it generates around 200 triggers and saves the measured intervals (using QueryPerformanceCounter) in a file. Can anyone tell me if its possible to get triggers every 5ms or so?
-
I have an application in which I require event triggers every 1-5ms or so. I am using C# and as far as I can tell, the usual triggering method in C# actually uses the DateTime.Now.Ticks property to generate event triggers. Thus its resolution is also limited to the resolution of DateTime.Now.Ticks which is 15.625 ms on my m/c (i.e. the smallest time interval whch DateTime.Now.Ticks can measure is 15.625 ms). If I set my trigger interval to anywhere between 1 - 15 ms I actually get triggers only after 15.625 ms. If I set my trigger interval to 16-31 ms I get triggers after every 31.25 ms and so on. I can email you a .cs file that illustrates this point. If you run the program it asks you the trigger interval you want to set. Then it generates around 200 triggers and saves the measured intervals (using QueryPerformanceCounter) in a file. Can anyone tell me if its possible to get triggers every 5ms or so?
First, I don't think that you mean miliseconds (ms). 1000 miliseconds are one second, so you are saying that it only triggers every 15 seconds(!). I suppose you mean nanoseconds (or something alike, something small). Second, much work is done 'behind the screen' when you do something in your program. Therefor, I suggest that you use a (
for
,do
,while(true)
) loop to get what you want. You might even want to place the loop on a separate thread. For the rest, I think that getting the intervalls lower is just a matter of optimization (i.e. usingunsafe
code blocks, maybe even P/Invoke to some Windows API). - Daniël Pelsmaeker"I will quote you when you say something memorable."
-
I have an application in which I require event triggers every 1-5ms or so. I am using C# and as far as I can tell, the usual triggering method in C# actually uses the DateTime.Now.Ticks property to generate event triggers. Thus its resolution is also limited to the resolution of DateTime.Now.Ticks which is 15.625 ms on my m/c (i.e. the smallest time interval whch DateTime.Now.Ticks can measure is 15.625 ms). If I set my trigger interval to anywhere between 1 - 15 ms I actually get triggers only after 15.625 ms. If I set my trigger interval to 16-31 ms I get triggers after every 31.25 ms and so on. I can email you a .cs file that illustrates this point. If you run the program it asks you the trigger interval you want to set. Then it generates around 200 triggers and saves the measured intervals (using QueryPerformanceCounter) in a file. Can anyone tell me if its possible to get triggers every 5ms or so?
fd97207 wrote: Can anyone tell me if its possible to get triggers every 5ms or so? Although I've never tinkered around with them myself, you should take a look a multimedia timers. They were designed for this kind of resolution. I think you're still going to have a problem with the garbage collector. Sooner or later it's going to want to do its business and halt every thread in your application.
-
First, I don't think that you mean miliseconds (ms). 1000 miliseconds are one second, so you are saying that it only triggers every 15 seconds(!). I suppose you mean nanoseconds (or something alike, something small). Second, much work is done 'behind the screen' when you do something in your program. Therefor, I suggest that you use a (
for
,do
,while(true)
) loop to get what you want. You might even want to place the loop on a separate thread. For the rest, I think that getting the intervalls lower is just a matter of optimization (i.e. usingunsafe
code blocks, maybe even P/Invoke to some Windows API). - Daniël Pelsmaeker"I will quote you when you say something memorable."
i meant what i wrote: 15.625 ms. Note the decimal point. Do you know any way I can change the resolution of DateTime.Now.Ticks so it can measure finer intervals?