How to stop the timer running in one viewmodel in another viewmodel?

Main viewmodel

class MainWindow { public void SetTimer(object sender, RoutedEventArgs e) { System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer(); timer.Tick += new EventHandler(timerTick); timer.Interval = new TimeSpan(0, 0, 5); timer.Start(); } private void timerTick(object sender, EventArgs e) { Application.Current.Windows[0].Close(); } } 

Child viewmodel

 class ChildWindow{ public void DoSomething(){ // что-то происходит // тут должен выключаться таймер } } 

In general, the idea is that I turn on the timer in one window and when I click a button, a new window opens in which the timer opened in the first window should turn off

  • It may be possible to pass to the ViewModel instance of the DispatcherTimer class from the main ViewModel . Just at the moment of creation and initialization of the child ViewModel . Give some code so that you can give a more specific answer. There are ways to link models ViewModel stackoverflow.com/a/5732844/5275890 - Denis Bubnov

1 answer 1

The easiest way to call on the Stop timer:

 DispatcherTimer timer; public void SetTimer(object sender, RoutedEventArgs e) { timer = new DispatcherTimer(); timer.Tick += new EventHandler(timerTick); timer.Interval = new TimeSpan(0, 0, 5); timer.Start(); } public void StopTimer() { timer.Stop(); }