0

I don't know how to explain this clearly but I will try my best. I'm using a DispatcherTimer class in my windows phone 8 project. I've got some check boxes and when a check box is checked and the button is pressed the countdown from a bus schedule starts. Everything works perfectly here but when I uncheck the check box and when I check another one, the same bus time starts and it goes down(countdown) by 2 seconds now. If I uncheck the check box and check another again the same time starts to countdown and the countdown goes by 3 seconds... I don't understand how, because on the click_event there is always a new instance of the class. The only thing that solves the problem is to reload my wp8 page and check something again and press the button for the countdown to start. Note: I'm reading my times from a .txt file which are store on a folder within my project (using streamreader). Any suggestions?

Here is some code:

 CheckBox[] listchekbox;
 DispatcherTimer timer;
    private void Button_Click(object sender, RoutedEventArgs e)//BUTTON CLICK
    {
         timer = new DispatcherTimer();

        listchekbox = new CheckBox[4] { Check1, Check2, Check3, Check4 };
        if (Onlyoneclick(listchekbox, 0)) { Gettingbustime(path_names[0]); }
        else if (Onlyoneclick(listchekbox, 1)) { Gettingbustime(path_names[1]); }


        timer.Interval = new TimeSpan(0,0,1);
        timer.Tick += onclick;
        timer.Start();


    }

    private void onclick(object sender, EventArgs e) // TIMER EVENT COUNT
    {

        Countdown(bus1); // the parameter is the textbox where the result is displayed
        Countdown(bus2);
    }
    private bool Onlyoneclick(CheckBox[] itemselected,int id) // this is not so important
    {
        int itemcounter = 0;
        int falsecounter = 0;
        for (int i = 0; i < listchekbox.Length; i++)
        {
            if (itemselected[i].IsChecked == true && id == i)
            {
                itemcounter++;
            }
            else if (itemselected[i].IsChecked == true) { falsecounter++; }
        }
        if (itemcounter == 1 && falsecounter==0) { return true; }
        else { return false; }

    }
    public void Countdown(TextBlock tx) 
    {

        string minute = tx.Text.Substring(0, 2);
        string sekunde = tx.Text.Substring(3, 2);
        int minute1 = Convert.ToInt32(minute);
        int sekunde1 = Convert.ToInt32(sekunde);

        sekunde1 = sekunde1 - 1;
             if (sekunde1 < 0) 
             {
                 minute1 = minute1 - 1;
                 sekunde1 = 59;

             }
             if (minute1 < 0) 
             {

                 timer.Stop();

                 MessageBox.Show("Autobus left!");
             }
             if (sekunde1 < 10) { tx.Text = minute1.ToString() + ":0" + sekunde1.ToString(); }
             else { tx.Text = minute1.ToString() + ":" + sekunde1.ToString(); }

             if (minute1 < 10) { tx.Text = "0" + minute1.ToString() + ":" + sekunde1.ToString(); }

             if (minute1 < 10 && sekunde1 < 10) { tx.Text = "0" + minute1.ToString() + ":0" + sekunde1.ToString(); }

    }
EugenSunic
  • 13,162
  • 13
  • 64
  • 86
  • You are creating a *new* timer, you didn't stop the old one. It continues ticking. – Hans Passant Aug 07 '14 at 17:59
  • I did but it is not shown in the code. Seems that the timer class should only be created once, in the initial constructor of the class at that only solves the problem although I'm not sure why – EugenSunic Aug 07 '14 at 18:10

1 Answers1

1

Try removing this code from Button_Click:

timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0,0,1);
timer.Tick += onclick;

And add it instead to your constructor.

Muster Station
  • 504
  • 2
  • 9