Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Timer in xamarin app speeds up and slows down

Im creating ccountdown timer in xamarin.forms. enter image description here
The problem is when I pause the timer and next I’ll start the timer, it speeds up and slows down and it happens the whole time. It should goes every second but why does it speed up sometimes ever 2/4 seconds ?
The code below:

 bool Value =false;
 int counter =120;
 private void RunTimer(Boolean Value)
 {
    Device.StartTimer(TimeSpan.FromSeconds(1), () => 
  {
    If(Value)
    {
      counter--;
      if(counter <=0)
      {
        counter = 120;
        MainText.Text = dt.AddSeconds(counter).ToString("mm:ss");
      }
      MainText.Text = dt.AddSeconds(counter).ToString("mm:ss");
      
    }
    return true;
 }
 return false;
});
   
 

  private void Start_Pause(object sender, EventArgs e)
 {
   if(value == false)
   {
     RunTimer(true);
     Value = true;
   }
   else
   { 
     RunTimer(false);
     Value = false;
   }
 }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MainText.Text = dt.AddSeconds(counter).ToString("mm:ss"); <– This is incorrect. Computer timers are never precise. Instead compute the DateTime end value once, and recompute remaining time by subtracing DateTime.Now on every tick (instead of counting imprecise time periods yourself).

Change your code to something like this:

private void RunTimer(Boolean value)
{
    DateTime start = DateTime.Now;
    DateTime end   = start.AddSeconds( 120 );

    Device.StartTimer( TimeSpan.FromMilliseconds( 100 ), () => 
    {
        TimeSpan remaining = end - DateTime.Now;

        Device.BeginInvokeOnMainThread( () => 
        {
            this.MainText.Text = remaining.ToString("mm:ss");
        });
        
        return remaining >= TimeSpan.Zero;
    });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading