locked
Problem with DispatcherTimer

    Question

  • Hi I have a problem with the following code. I have a home page Page1 and another with a countdown Page2.

    The first time I traveled to Page2 the countdown decremented one by one seconds, but again Page1 and Page2 travel again and now the countdown accelerates, what happens?

    Page1:
    Call  page 2.

    Page2:

    TiempoHora = await GetTiempoRestante(objecto);
      TiempoExpirar = new TimeSpan(TiempoHora.Days, TiempoHora.Hours, TiempoHora.Minutes, TiempoHora.Seconds);

     public async void CreateTimer()
            {
                
                timer = new DispatcherTimer();
                 timer.Tick += timer_Tick;
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Start();    
                

            }

      async void timer_Tick(object sender, object e)
            {
                if (TiempoExpirar .TotalSeconds==0)
                {
                    timer.Stop();
                    tbTiempo.Text = "caducado";
                }
                else
                {
                  
                        TiempoExpirar = TiempoExpirar.Subtract(segundo);
                        string minutos = "";
                        string segundos = "";
                        string horas = "";

                        if (TiempoExpirar .Minutes < 10)
                            minutos = "0" + TiempoExpirar .Minutes.ToString();
                        else
                            minutos = TiempoExpirar .Minutes.ToString();
                        if (TiempoExpirar .Seconds < 10)
                            segundos = "0" + TiempoExpirar .Seconds.ToString();
                        else
                            segundos = TiempoExpirar .Seconds.ToString();

                        if (TiempoExpirar .Days > 0)
                            textTiempo = TiempoExpirar .Days + " d, " + TiempoExpirar .Hours + ":" + minutos + ":" + segundos;
                        else
                            textTiempo = TiempoExpirar .Hours + ":" + minutos + ":" + segundos;
                        
                        tbTiempo.Text = textTiempo;
                  
                }

            }

    Saturday, January 24, 2015 7:18 PM

Answers

  • Hi Sergio,

    Your code is not enough for me to reproduce the problem. Can you post a repro project here? User your OneDrive and share a link here.

    I assume you will create that DispatcherTimer object every time when the Page2 is loaded. Try stop that DispatcherTimer object in OnNavigatingFrom event handler in Page2. Code looks like the following.

     protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    
            {
    
                timer.Stop();
    
            }
    

    Please try using this and let me know the result.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, January 26, 2015 6:21 AM
    Moderator