none
Windows CE accurate time trigger event? RRS feed

  • Question

  • I have tried Threading.Timer, which is very inaccurate. for example

    timer = new Timer(new TimerCallback(doWork), new object(), 1000, 200);
    
    void doWork()
    {
       Console.Writeline(CurrentTime);  //ms
    }
    

    The doWork() method will be invoke multiple times within 200ms

    I know that Thread.sleep() is not an accurate time counter.

     

    I have read some book which said something about time counting resolution.

    One need to use the system API to get oscillator frequency and CPU clock, to calculate accurate time in millisecond.

    I have implement a StopWatch with these API, but don't know how to use it on Time trigger event.

     

    Please give me some advice, I need to do some automation, accurate to 1~5 ms would be fine 

    Tuesday, December 27, 2011 9:05 AM

Answers

  • Software timer is never going to be accurate... You need to use hardware timer indeed. What is the processor you are using? There is a general purpose timer your processor should be having. If you implement a driver for this timer and use it over the application to trigger the event. --- Misbah
    • Edited by Misbah Khan Tuesday, December 27, 2011 4:41 PM
    • Marked as answer by Maxi Wu Tuesday, January 10, 2012 1:25 AM
    Tuesday, December 27, 2011 4:40 PM
  • You don't care about jitter if all you are doing is counting time. There are two options and which one you choose will be dependent on the characteristics of your hardware, primarily:

    1. Use GetTickCount(), or System.Environment.TickCount, to get the number of milliseconds since the system started. When it's time to stop counting, get the tick count again. The elapsed time is the difference. Don't forget to account for roll-over in 32-bit values every month or so. Most non-x86 devices actually count 1ms every 1ms, so this should be fine for you.

    2. Use something like a hardware timer or a performance counter and do the same process as above, but with the much higher-resolution values. You could use a dedicated hardware counter to count clock ticks, for example, on your hardware to get something with a resolution of the clock rate of the board (100MHz?)

    You absolutely DO NOT want to use SetTimer() to try to count the passage of time. Forget about that. You can use a timer to periodically update the user interface, if you want, but the jitter in calling a timer function is enormous, far too much to maintain good time counting.

    "Real-time" has absolutely nothing whatsoever to do with "time keeping". Real time is associated with the guaranteed delay between some hardware event and a software response to that event in the operating system. Windows CE is generally a real-time system.

    Paul T.

    • Marked as answer by Maxi Wu Tuesday, January 10, 2012 1:25 AM
    Monday, January 9, 2012 9:09 PM

All replies

  • Software timer is never going to be accurate... You need to use hardware timer indeed. What is the processor you are using? There is a general purpose timer your processor should be having. If you implement a driver for this timer and use it over the application to trigger the event. --- Misbah
    • Edited by Misbah Khan Tuesday, December 27, 2011 4:41 PM
    • Marked as answer by Maxi Wu Tuesday, January 10, 2012 1:25 AM
    Tuesday, December 27, 2011 4:40 PM
  • It also appears that you are doing this in managed code, which may cause problems for you when the GC kicks in.
    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com
    Tuesday, December 27, 2011 8:06 PM
    Moderator
  • Step back and tell us what you're trying to accomplish, rather than how you've tried to do it. You want to measure the amount of elapsed time since some start event (stopwatch)? Or you need to perform some operation at a regular interval with little timing jitter (scheduling an event)? The answer to each of those questions will be very different.

    Paul T.

    Tuesday, December 27, 2011 8:08 PM
  • Step back and tell us what you're trying to accomplish, rather than how you've tried to do it. You want to measure the amount of elapsed time since some start event (stopwatch)? Or you need to perform some operation at a regular interval with little timing jitter (scheduling an event)? The answer to each of those questions will be very different.

    Paul T.

    "perform some operation at a regular interval with little timing jitter "

    This is what I need, I am ok to do timekeeping(stopwatch), don't know how to use it to trigger little time jitter.

    Worst approach I could come up is busy wait... but that couldn't be good to multi threading program I suggest.

    while(true)
    {
         sum += StopWatch.Stop();
         if(sum>=triggerThreshold)
             {
                     //fire event
                     //reset sum
              }
    }
    


    Please advice.

    Thank you Paul.

    Wednesday, December 28, 2011 2:10 AM
  • Yes, I am doing this in managed code, Bruce.

    I wrap system API to do time keeping

    [DllImport("coredll.dll")]
            public static extern bool QueryPerformanceCounter(out long c);
            [DllImport("coredll.dll")]
            public static extern bool QueryPerformanceFrequency(out long c);
    


    Managed code threading may cause some jitter, but I hope it is within my suggested range, 10ms maybe.

    I think GC would do it's job out of my way (threading priority maybe?)

    What do you think on GC's impact to time keeping?

    Wednesday, December 28, 2011 2:17 AM
  • processor is PXA270. I think the device has an external hardware timer too.

    Now that you mention it, system API QueryPerformanceCounter() should be using hardware, that's what msdn tells me.

    But, that external hardware timer should be more reliable, I think.

    They have indeed provide a driver API to Set Timer and Interrupt would be fire when the time is up.

    I suppose I should do some test on that hardware timer and then get back to you later.

     

    That rise an interesting question, WinCE is suppose to be a RTOS, What we are doing have nothing to do with WinCE.

    So, What position does WinCE stand when it comes to time keeping?

     

    Thank you  Misbah.


    • Edited by Maxi Wu Wednesday, December 28, 2011 2:31 AM
    Wednesday, December 28, 2011 2:30 AM
  • You don't care about jitter if all you are doing is counting time. There are two options and which one you choose will be dependent on the characteristics of your hardware, primarily:

    1. Use GetTickCount(), or System.Environment.TickCount, to get the number of milliseconds since the system started. When it's time to stop counting, get the tick count again. The elapsed time is the difference. Don't forget to account for roll-over in 32-bit values every month or so. Most non-x86 devices actually count 1ms every 1ms, so this should be fine for you.

    2. Use something like a hardware timer or a performance counter and do the same process as above, but with the much higher-resolution values. You could use a dedicated hardware counter to count clock ticks, for example, on your hardware to get something with a resolution of the clock rate of the board (100MHz?)

    You absolutely DO NOT want to use SetTimer() to try to count the passage of time. Forget about that. You can use a timer to periodically update the user interface, if you want, but the jitter in calling a timer function is enormous, far too much to maintain good time counting.

    "Real-time" has absolutely nothing whatsoever to do with "time keeping". Real time is associated with the guaranteed delay between some hardware event and a software response to that event in the operating system. Windows CE is generally a real-time system.

    Paul T.

    • Marked as answer by Maxi Wu Tuesday, January 10, 2012 1:25 AM
    Monday, January 9, 2012 9:09 PM
  • Thank you so much Paul.

    your explanation let me have a better understanding on RTOS and SetTime().

    These information are critical. 

    Tuesday, January 10, 2012 1:26 AM