High resolution timer?
-
Already read and tested, with this I can only get 1 millisecond resolution (Multimedia Timer), but I need 1 microsecond...
-- Everything is possible, even the impossible! ^_^
If you need to control timing that tightly, then you need a real time OS. There are several flavors of *nix that are, but windows is not.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
Hello, In my application, I have to get a microsecond resolution for a timer. Is it possible to do (on WinXP or Vista) ? There should be high precision timers in kernel mode, but what is it and how could I get to them from .Net code? All help will be appreciated. Roman
-- Everything is possible, even the impossible! ^_^
-
If you need to control timing that tightly, then you need a real time OS. There are several flavors of *nix that are, but windows is not.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
Yeah, it's a good solution for measuring time, but for waiting (sleeping) I wasn't able to find a way to use it. Thread.Sleep in combination with winmm.dll calls give 1ms precision only :/
-- Everything is possible, even the impossible! ^_^
Hi Kel_, regular threads cant be scheduled at such precise moments. The scheduler basically works with the system timer (thats around 16-20 msec) and it may be locked by something, and hence take tens of microseconds to perform a switch; you can improve in two ways: 1. the hacking way: give yourself a high priority, do a busy wait, then go for it; as a general solution, it stinks; the other threads and processes wont like you and neither will the user in front of the PC; if you only need it occasionally (say you did cause an external action to start and need to measure a micro-switch very accurately, then react on it), you might even raise your thread priority to real-time. Dont do this for more than a few milliseconds, or your PC will seem dead (and may even need a reboot). Even so, I dont think you will succeed each time; at best I expect it to work most of the time. 2. the normal way: write a driver; use interrupts, and run your ISR at one of the interrupt priorities, then do postprocessing at one of the real-time priorities, then signal the user (at a normal priority). This involves quite some work and knowledge; I have done this often on embedded systems, never on a PC. And the more evolved the Windows version, the harder it gets; they keep adding protections and barriers... :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
:rose::rose::rose: I am confident it is not; it probably is adequate for timing software events. But who ever is interested in interfacing to the outside world, or adding some special hardware to a PC, will ask things like the OP did. And since there is no perfect solution available yet, MS is likely to continue and improve on it step-by-step, as usual. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi Kel_, regular threads cant be scheduled at such precise moments. The scheduler basically works with the system timer (thats around 16-20 msec) and it may be locked by something, and hence take tens of microseconds to perform a switch; you can improve in two ways: 1. the hacking way: give yourself a high priority, do a busy wait, then go for it; as a general solution, it stinks; the other threads and processes wont like you and neither will the user in front of the PC; if you only need it occasionally (say you did cause an external action to start and need to measure a micro-switch very accurately, then react on it), you might even raise your thread priority to real-time. Dont do this for more than a few milliseconds, or your PC will seem dead (and may even need a reboot). Even so, I dont think you will succeed each time; at best I expect it to work most of the time. 2. the normal way: write a driver; use interrupts, and run your ISR at one of the interrupt priorities, then do postprocessing at one of the real-time priorities, then signal the user (at a normal priority). This involves quite some work and knowledge; I have done this often on embedded systems, never on a PC. And the more evolved the Windows version, the harder it gets; they keep adding protections and barriers... :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Thank you! This is helpful :) So I should look for creating a driver, I'm interested in some examples... For the work and knowledge, I have my time and I like to learn. Just for the context: my application is XNA flight simulator for a real world helicopter.
-- Everything is possible, even the impossible! ^_^
-
Thank you! This is helpful :) So I should look for creating a driver, I'm interested in some examples... For the work and knowledge, I have my time and I like to learn. Just for the context: my application is XNA flight simulator for a real world helicopter.
-- Everything is possible, even the impossible! ^_^
OK, I dont remember having seen a single CP article on drivers. And IIRC the first thing you will need is called a DDK (Drivers Development Kit). I also suggest you trim down your PC as much as you can: since you are going to reboot your PC every time you hit the RUN button, better have the fastest boot time possible. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hello, In my application, I have to get a microsecond resolution for a timer. Is it possible to do (on WinXP or Vista) ? There should be high precision timers in kernel mode, but what is it and how could I get to them from .Net code? All help will be appreciated. Roman
-- Everything is possible, even the impossible! ^_^
You could always look at
rdtsc ( ReaD TimeStamp Counter )
instruction which gives you a 64bit integer of how many clock cycles have passed since the computer was started up. To use it, you would have to write a .dll in C++ with inline assembly language, and call the .dll from your .NET app. Since it is in terms of clock cycles, it might be too fine of a measurement, but just a thought..."Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
Yeah, but it should work in windows. I'm sure there's something like nanosleep in win32 kernel mode.
-- Everything is possible, even the impossible! ^_^
Kel_ wrote:
I'm sure there's something like nanosleep in win32 kernel mode.
No, there isn't.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007