title: Timer counting in Win8 app
body: <p>I am building a win8 app which has 3 buttons(Button1, Button2 and StartButton) and timer in it. Button1 and Button2 are disabled. If clicked on StartButton, Button1 is enabled and the number of clicks within 20 secs is counted and displayed in a textblock1. After the timer is over Button1 is disabled and Button2 is enabled and clicks are counted and displayed in textblock2. My problem is that the timer ticks properly for Button1 and not for Button2. Timer becomes faster when button2 is enabled. Can someone help me?
  My code is below:</p>
  
  <pre><code>    private int count1=0;
      private int count2=0;
      private int clickCounter = 0;
      private int timeLeft;
      private DispatcherTimer timer;
  
      private void StartTimer()
      {
          if (this.timer != null)
          {
              this.StopTimer();
          }
          DispatcherTimer timer = new DispatcherTimer();
          timer.Interval = new TimeSpan(0,0,0,1);
          timer.Tick += timer_Tick;
          timer.Start();
  
      }
  
      private void StopTimer()
      {
          if (this.timer != null)
          {
              this.timer.Stop();
              this.timer = null;
          }
      }
  
      public void timer_Tick(object sender, object args)
      {
  
          if (timeLeft &gt; 0)
          {
              timeLeft = timeLeft - 1;
              timerTextBlock.Text = Convert.ToString(timeLeft);
              this.StartButton.IsEnabled = false;
          }
          else
          {
              StopTimer();
              if (clickCounter==2)
              {
                  ShowResult();
                  this.Button2.IsEnabled = false;
                  this.StartButton.IsEnabled = false;
              }
              else
              {
                  myMsg.Text = "Time's up!";
                  this.Button1.IsEnabled = false;
                  this.StartButton.IsEnabled = true;
              }
          }
      }
  
      private void Button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
      {
          // TODO: Add event handler implementation here.
          count1++;
          this.textblock1.Text=count1.ToString();
  
      }
  
      private void Button2_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
      {
          count2++;
          this.textblock2.Text=count2.ToString();
      }
      public void ResetTimer()
      {
              timeLeft = 20;
      }
      private void StartButton_Click(object sender, RoutedEventArgs e)
      {
          clickCounter++;
          if (textblock1.Text == "0")
          {
              ResetTimer();
              StartTimer();
              this.Button1.IsEnabled = true;
          }
          else
          {
              ResetTimer();
              StartTimer();
              this.Button2.IsEnabled = true;
          }
      }
  </code></pre>