1

i am working on WP8 , i am using timer where i am calulating the total time.

This is how i am doing it:

timer = new DispatcherTimer();
 timer.Interval = TimeSpan.FromSeconds(1);//interval for timer is 1 sec
 timer.Tick += new EventHandler(timer_Tick);//after the timer expires, this event is fired
 timer.Start();
 startDateTime = DateTime.Now;  

 DateTime et = DateTime.Now;

   Debug.WriteLine("st is "+startDateTime+ " et is "+et);

   TimeSpan myDateResult = et - startDateTime;
   double seconds = myDateResult.TotalSeconds;
   Debug.WriteLine("difference is " + seconds);

Output which i am getting :

st is 3/25/2014 3:54:09 PM et is 3/25/2014 3:55:14 PM
difference is 64.312636

So i run completely for 1 min then its giving me 64 sec as output. why so? is this a bug ?

EDIT

Here timerCount value is 60;

private void timer_Tick(object sender, EventArgs e)
        {                                            
            timerCount--;
            if (timerCount > 0)
            {
                TimerText.Text = "Timer : "+timerCount.ToString();
            }
            else
            {
                TimerText.Text = "Timer : ";

            }            
        }
user2056563
  • 600
  • 2
  • 12
  • 37

2 Answers2

1

So you are counting ticks with this timer and expecting after 60 ticks of 1s, exactly 1 minute will have passed.

It doesn't work like that. Other stuff is happening in the application, and .NET never assures it will be exactly one second between ticks.

If you want accuracy, use System.Diagnostics.Stopwatch which gives a very accurate elapsed time based on the system clock.

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
  • Can you please give me some eaxmple or links that show me how to use it – user2056563 Mar 25 '14 at 10:49
  • Is Stopwatch supported in wp8? – user2056563 Mar 25 '14 at 10:51
  • It appears to be, confirmed via this link http://blogs.microsoft.co.il/alex_golesh/2012/11/05/whats-new-in-windows-phone-8-5-out-of-8quick-tip-creating-hybrid-applications/ – Chris Ballard Mar 25 '14 at 10:54
  • Can you please help me to implement start time, interval and end time and remaining time calculations(like what i have shown in question) using stopwatch ? – user2056563 Mar 27 '14 at 09:08
  • If you are looking to have events raised with high precision (especially on WP) you are going to have an uphill struggle. See http://stackoverflow.com/questions/4212611/raise-event-in-high-resolution-interval-timer/4213950#4213950 for a detailed explaination - especially the second answer – Chris Ballard Mar 27 '14 at 09:35
  • Perhaps you could take a step back and explain what your scenario is, there may be an alternative approach we can recommend? – Chris Ballard Mar 27 '14 at 09:36
  • Ok, can you please invite me to a private chat room so that we can discuss on this,its very simple and i its talking very long time to fix this – user2056563 Mar 27 '14 at 09:39
  • as i am not able to create a chart room, i request you to please help me on this – user2056563 Mar 27 '14 at 09:41
1

You are using the wrong tool for calculating time. You should StopWatch instead of timer. Timer is used to do something that is meant to happen at a regular interval.

Ehsan
  • 31,833
  • 6
  • 56
  • 65