Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to stop timer from another window but i am getting exception as
Object reference not set to an instance of an object.
My code is
MainWindow.xaml
public  DispatcherTimer timer = new DispatcherTimer();

       public MainWindow()
       {
           InitializeComponent();

           timer.Interval = TimeSpan.FromSeconds(1);
           timer.Tick += timer_Tick;
           timer.Start();

       }

       void timer_Tick(object sender, EventArgs e)
       {
           lblTime.Content = DateTime.Now.ToLongTimeString();
       }

       private void Button_Click(object sender, RoutedEventArgs e)
       {
           Window1 obj = new Window1();
           obj.ShowDialog();
       }


Window1.xaml
public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainWindow obj = new MainWindow();
            obj = Application.Current.Windows.OfType<MainWindow>().SingleOrDefault(x => x.IsActive);
            obj.timer.Stop();
        }
Posted

1 solution

I solved using ..

((MainWindow)(this.Owner)).timer.Stop();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900