How to use dispatcherTimer.Stop in another function?

I have a question regarding the use of dispatcherTimer in code. See 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, I start the timer, and then in the tick I do some things when the timer reaches ten seconds. However, I want to stop the dispatcher in this if statement, but then I get a contextual error.

So, how do I stop the timer in another function?

Update: I tried to fit into your solution, but I get a link to an object not set to an instance error

 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(); } 
+4
source share
2 answers

You can use a private field inside your class to contain an instance of DispatcherTimer. Then you can access it in every non-stationary method of your class.

EDIT: Add Sample

To give you a sample, I developed a simple WPF application containing a TextBlock, the contents of which are updated every second. After five seconds, the timer turns off. This is XAML:

 <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBlock Text="{Binding Path=Counter}" /> </Grid> </Window> 


And this is the code:

 public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); this.DataContext = this; dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Start(); } private DispatcherTimer dispatcherTimer; private int counter; public int Counter { get { return counter; } set { counter = value; OnPropertyChanged("Counter"); } } private void dispatcherTimer_Tick(object sender, EventArgs e) { Counter++; if (Counter == 5) { dispatcherTimer.Stop(); dispatcherTimer = null; } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler e = PropertyChanged; if (e != null) { e(this, new PropertyChangedEventArgs(propertyName)); } } } 


Hope this helps. Best wishes.

+5
source

it 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(); } 
0
source

All Articles