---
title: "How to use dispatcherTimer.Stop in a different function?"  
description: "How to use dispatcherTimer.Stop in a different function?"  
author: "Anonymous User"  
published: 2014-01-22  
updated: 2014-01-23  
canonical: https://www.mindstick.com/forum/1857/how-to-use-dispatchertimer-stop-in-a-different-function  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to use dispatcherTimer.Stop in a different function?

I have a [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) regarding the use of dispatcherTimer in code. Please look at my situation below:

```
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);        }
```

As you can see in the top [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) I start the [timer](https://www.mindstick.com/articles/52/creating-timer-at-runtime-in-c-sharp-dot-net) and then in the Tick I do some [stuff](https://www.mindstick.com/interview/33816/what-is-stuff-function-is-sql-server) when the timer reaches [ten](https://answers.mindstick.com/qa/103981/what-is-the-ten-commandments) seconds. However I want to stop the dispatchertimer in that [if statement](https://www.mindstick.com/forum/12682/jquery-toggle-if-statement) but then I get a [context](https://www.mindstick.com/forum/23245/what-is-context-in-android) error.

So how do I stop the timer in a different function?

## Replies

### Reply by Pravesh Singh

Hi Jay,\

this works :)

```
    public MainWindow()    {       
InitializeComponent();       
DispatcherTimer dispatcherTimer = new DispatcherTimer();       
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);       
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);       
dispatcherTimer.Start();    }    private void dispatcherTimer_Tick(object sender, EventArgs e)    {             //Do Something    }    private void Button_Click_1(object sender, RoutedEventArgs e)    {       
Dispatcher.InvokeShutdown();    }
```


---

Original Source: https://www.mindstick.com/forum/1857/how-to-use-dispatchertimer-stop-in-a-different-function

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
