Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. High resolution timer?

High resolution timer?

Scheduled Pinned Locked Moved C#
questioncsharphelp
14 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kel_
    wrote on last edited by
    #1

    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! ^_^

    P G E P 4 Replies Last reply
    0
    • K Kel_

      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! ^_^

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Luc's article[^] is probably the definitive article on the subject.

      Deja View - the feeling that you've seen this post before.

      K L 2 Replies Last reply
      0
      • K Kel_

        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! ^_^

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Use the System.Diagnostics.Stopwatch class.

        --- single minded; short sighted; long gone;

        K 1 Reply Last reply
        0
        • P Pete OHanlon

          Luc's article[^] is probably the definitive article on the subject.

          Deja View - the feeling that you've seen this post before.

          K Offline
          K Offline
          Kel_
          wrote on last edited by
          #4

          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! ^_^

          D 1 Reply Last reply
          0
          • G Guffa

            Use the System.Diagnostics.Stopwatch class.

            --- single minded; short sighted; long gone;

            K Offline
            K Offline
            Kel_
            wrote on last edited by
            #5

            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! ^_^

            L 1 Reply Last reply
            0
            • K Kel_

              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! ^_^

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #6

              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

              K 1 Reply Last reply
              0
              • K Kel_

                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! ^_^

                E Offline
                E Offline
                Ed Poore
                wrote on last edited by
                #7

                Isn't there a timer which uses Performance Counters or something like that?


                My Blog[^]

                1 Reply Last reply
                0
                • D Dan Neely

                  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

                  K Offline
                  K Offline
                  Kel_
                  wrote on last edited by
                  #8

                  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! ^_^

                  D 1 Reply Last reply
                  0
                  • K Kel_

                    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! ^_^

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    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] }


                    K 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Luc's article[^] is probably the definitive article on the subject.

                      Deja View - the feeling that you've seen this post before.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      :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] }


                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        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] }


                        K Offline
                        K Offline
                        Kel_
                        wrote on last edited by
                        #11

                        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! ^_^

                        L 1 Reply Last reply
                        0
                        • K Kel_

                          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! ^_^

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          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] }


                          1 Reply Last reply
                          0
                          • K Kel_

                            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! ^_^

                            P Offline
                            P Offline
                            Paul Conrad
                            wrote on last edited by
                            #13

                            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

                            1 Reply Last reply
                            0
                            • K Kel_

                              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! ^_^

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              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

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups