0

So, i got an application (windows phone 7.5 over c#) using an DispatcherTimer to emulate a timer. I have set the interval to 1 millisecond:

timer.Interval = new TimeSpan(0,0,0,0,1);

Then i am declaring a TimeSpan in order to make a simple countdown:

TimeSpan countdown = TimeSpan.FromSeconds(10);

On each tick im Substracting 1 millisencond:

countdown = countdown.Subtract(TimeSpan.FromMilliseconds(1));

In Visual Studio's windows phone emulator 256/512 its working with 'no problems', but when i run it on my phone (LG Quantum) it seems to have precision problems. I have compared it with my PC screen running the application and its way slower on the phone. I have tried to not to run the application in debug mode in my phone. Is there any solution to this? Or i have to take another approach?

damuz91
  • 1,461
  • 2
  • 21
  • 37
  • What exactly are you trying to accomplish? Timers are always tricky with little intervals. – Dan Abramov Jun 10 '12 at 21:54
  • I need to display a countdown (seconds:milliseconds) with milliseconds precision. – damuz91 Jun 10 '12 at 22:07
  • 2
    It seems that firing an event takes more than a millisecond. Therefore, your best bet is to [pretend to have milliseconds precision](http://stackoverflow.com/a/10972639/458193). – Dan Abramov Jun 10 '12 at 22:09

2 Answers2

6

A different approach would be better here:

  1. Record the current time when you start the timer

    DateTime startTime = DateTime.Now;
    
  2. Each time your timer ticks, recalculate the countdown

    TimeSpan countdown = DateTime.Now - startTime;
    
Eric
  • 95,302
  • 53
  • 242
  • 374
  • Ok, i got more precision with your approach (and its the solution), but could you explain why substracting DateTime's objects is better than substracting from TimeSpan? – damuz91 Jun 10 '12 at 22:36
  • Because my solution doesn't depend on the period of the DispatchTimer – Eric Jun 11 '12 at 07:06
  • The rest of the solution would be to readjust the `DispatcherTimer` delay consequently, so if a task is attached to the triggering of the tick, it is more or less executed on an accurate time. – Léon Pelletier Jun 24 '13 at 07:58
  • can you please help me on this http://stackoverflow.com/questions/22632006/timer-start-time-and-end-time-calculation-gives-4-secs-extra – user2056563 Mar 27 '14 at 10:45
3

Experience tells me that you're simply trying to fire far too many events for the phone hardware to handle.

From MSDN:

The DispatcherTimer is reevaluated at the top of every DispatcherTimer loop.

Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs.

Remember that the actual phone hardware is single core so trying to capture precisely 1000 events per second isn't the best approach.

Either increase the time between ticks and increment based on the new interval, or take the time the timer is started and subtract it from the current time each tick.

Graham Wager
  • 1,887
  • 1
  • 19
  • 28
  • Excelent explanation, also we have to think that windows phone may not be running as many applications as an android phone in the background, but still have to run basic phone tasks. Thanks. – damuz91 Jun 10 '12 at 22:35