title: C# Reset a countdown timer-DispatcherTimer- in windows store app
body: <p>I'm a C# newbie_and in programming in general_ and Ι'm trying to build a math quiz app with a countdown timer.</p>
  
  <p>I generate an equation each time the user clicks the start button and I give him a max 60 seconds to answer. The user answers -whether his answer is wrong or right doesn't matter_ and can he/she can click again for a new equation. So I want the timer to reset each time the user is shown a new random equation. So far I've only managed to reset this when the 60sec timespan elapses but even that is not working properly, sometimes it displays 59 or 58 secs instead of 60. </p>
  
  <p>So far reading other questions has't helped me much and the timer confuses me. I also accept suggestions to make my code simpler and more elegant.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>EquationView.xaml.cs</strong></p>
  
  <pre><code>public sealed partial class EquationView : Page
      {
          DispatcherTimer timer = new DispatcherTimer();
          int tick = 60;
          int result;
  
          public EquationView()
          {
              this.NavigationCacheMode = NavigationCacheMode.Enabled;
              this.InitializeComponent();
          }
  
          private void startButton_Click(object sender, RoutedEventArgs e)
          {
              // Once clicked then disabled
              startButton.IsEnabled = false;
  
              // Enable buttons required for answering 
              resultTextBox.IsEnabled = true;
              submitButton.IsEnabled = true;
  
              var viewModel = App.equation.GenerateEquation();
              this.DataContext = viewModel;
              result = App.equation.GetResult(viewModel);
  
              timer.Interval = new TimeSpan(0, 0, 0, 1);
              //timer.Tick += new EventHandler(timer_Tick);
              timer.Tick += timer_Tick;
              timer.Start();
              DateTime startTime = DateTime.Now;
  
              // Reset message label
              if (message.Text.Length &gt; 0)
              {
                  message.Text = "";
              }
  
              // Reset result text box
              if (resultTextBox.Text.Length &gt; 0)
              {
                  resultTextBox.Text = "";
              }
          }
  
          private void timer_Tick(object sender, object e)
          {
              Countdown.Text = tick + " second(s) ";
              if (tick &gt; 0)
                  tick--;
              else
              {
                  Countdown.Text = "Times Up";
                  timer.Stop();
                  submitButton.IsEnabled = false;
                  resultTextBox.IsEnabled = false;
                  startButton.IsEnabled = true;
                  tick = 60;
              }
  
          }
  
          private void submitButton_Click(object sender, RoutedEventArgs e)
          {
              timer.Stop();
              submitButton.IsEnabled = false;
              resultTextBox.IsEnabled = false;
  
              if (System.Text.RegularExpressions.Regex.IsMatch(resultTextBox.Text, "[^0-9]"))
              {
                  MessageDialog msgDialog = new MessageDialog("Please enter only numbers.");
                  msgDialog.ShowAsync();
  
                  resultTextBox.Text.Remove(resultTextBox.Text.Length - 1);
  
                  //Reset buttons to answer again
                  submitButton.IsEnabled = true;
                  resultTextBox.IsEnabled = true;
                  timer.Start();
              }
              else
              {
                  try
                  {
                      int userinput = Int32.Parse(resultTextBox.Text);
  
                      if (userinput == result)
                      {
                          message.Text = "Bingo!";
                          App.player.UpdateScore();
                          startButton.IsEnabled = true;
                      }
                      else
                      {
                          message.Text = "Wrong, sorry...";  
                          startButton.IsEnabled = true;
                      }
                  }
                  catch (Exception ex)
                  {
                      MessageDialog msgDialog = new MessageDialog(ex.Message);
                      msgDialog.ShowAsync();
                      submitButton.IsEnabled = true;
                      resultTextBox.IsEnabled = true;
                      timer.Start();
                  }
  
  
              }
          }
  </code></pre>