title: How to use dispatcherTimer.Stop in a different function?
body: <p>I have a question regarding the use of dispatcherTimer in code. Please look at my situation below:</p>
  
  <pre><code>private void CheckShow(object sender, System.Windows.RoutedEventArgs e)
  {
      DispatcherTimer dispatcherTimer = new DispatcherTimer();
  
      dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
      dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);             
      dispatcherTimer.Start();
      string etime = DateTime.Now.Second.ToString();      
  }
  
  private void dispatcherTimer_Tick(object sender, EventArgs e)
  {
      if(System.IO.File.Exists(@"C:\Default.xml"))
      {
          LoadingRecent.Text = "Loading Default Show...";
          LoadBar.Opacity = 100;
          string time1 = DateTime.Now.Millisecond.ToString();
          string time2 = DateTime.Now.Second.ToString();
          double huidigetijd = System.Convert.ToDouble(time2 + "." + time1);
          LoadBar.Value = huidigetijd;
          Remainingnummer.Text = Convert.ToString(10 - DateTime.Now.Second);
  
          string etime = DateTime.Now.Second.ToString();
          if (etime == "10")
          {
              var provider = (XmlDataProvider)this.Resources["CUEData"]; 
              var loadfilepath = @"C:\Default.xml";
              provider.Source = new Uri(loadfilepath, UriKind.Absolute);
              Storyboard Hoofdvenster = (Storyboard)Resources["Hoofdvenster"];
              Hoofdvenster.Begin(this, true);
          }
  </code></pre>
  
  <p>As you can see in the top function I start the timer and then in the Tick I do some stuff when the timer reaches ten seconds. However I want to stop the dispatchertimer in that if statement but then I get a context error.</p>
  
  <p>So how do I stop the timer in a different function?</p>
  
  <p>Update: I tried to fit in your solution but I get an object reference not set to an instance error</p>
  
  <pre><code>public void CheckShow(object sender, System.Windows.RoutedEventArgs e)
  {
      DispatcherTimer dispatcherTimer = new DispatcherTimer();
  
      dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
      dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);             
      dispatcherTimer.Start();
      string etime = DateTime.Now.Second.ToString();
  
      if (etime == "13")
      {
          dispatcherTimer.Stop();
          LoadingRecent.Text = "You are currently working on a show. Press New or Load to create or load a different show";
          LoadBar.Opacity = 0;
      }
  }
  private DispatcherTimer dispatcherTimer;    
  private void dispatcherTimer_Tick(object sender, EventArgs e)
  {
      if(System.IO.File.Exists(@"C:\Default.xml"))
      {
          LoadingRecent.Text = "Loading Default Show...";
          LoadBar.Opacity = 100;
          string time1 = DateTime.Now.Millisecond.ToString();
          string time2 = DateTime.Now.Second.ToString();
          double huidigetijd = System.Convert.ToDouble(time2 + "." + time1);
          LoadBar.Value = huidigetijd;
          Remainingnummer.Text = Convert.ToString(10 - DateTime.Now.Second);
  
          string etime = DateTime.Now.Second.ToString();
          if (etime == "10")
          {
              var provider = (XmlDataProvider)this.Resources["CUEData"]; 
              var loadfilepath = @"C:\Default.xml";
              provider.Source = new Uri(loadfilepath, UriKind.Absolute);
              Storyboard Hoofdvenster = (Storyboard)Resources["Hoofdvenster"];
              Hoofdvenster.Begin(this, true);
              dispatcherTimer.Stop(); 
          }
  </code></pre>