Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two windows called MainWindow and Window1. There are methods which starts(after button click(btn_Start_Click)) and displays timer(after click btn_Stop_Click) inside the textblock(display_1) in MainWindow. And I have stop button in Window1. I want to display this finally time(after stop button clicked in window1) and display this in Window1. But, when I click btn_Stop_Click in Window1 it doesnt work. And how can I display finally time(after click stop button) in Window1? Thank you.
Here is the following code:

What I have tried:

MainWindow.xaml.cs:
C#
namespace WpfApplication9
{
    public partial class MainWindow : Window
    {
        DispatcherTimer dt = new DispatcherTimer();
        Stopwatch sw = new Stopwatch();
        string currentTime = string.Empty;
 
        public MainWindow()
        {
            InitializeComponent();
            dt.Tick += new EventHandler(dt_Tick);
            dt.Interval = new TimeSpan(0,0,0,0,1);
        }
 
        public void dt_Tick(object sender, EventArgs e)
             {
                if (sw.IsRunning)
                {
                    TimeSpan ts = sw.Elapsed;
                    currentTime = String.Format("{0:00}:{1:00}:{2:00}",
                    ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
                    display_1.Text = currentTime;
                }
            }
 
        public void Stop_time()
        {
            if (sw.IsRunning)


Window1.xaml.cs:
C#
namespace WpfApplication9
{
 
    public partial class Window1 : Window
    {
 
        public Window1()
        {
            InitializeComponent();
        }
 
        private void btn_Stop_Click(object sender, RoutedEventArgs e)
        {
            MainWindow ob = new MainWindow();
            ob.Stop_time();
        }
 
        private void btn_Close_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }
 
        private void btn_show_Click(object sender, RoutedEventArgs e)
        {
 
        }
 

    }


Thank You!
Posted
Updated 14-Mar-16 9:16am

1 solution

Um, where to start?
First let's review objects and instances.
When you do this:
C#
MainWindow ob = new MainWindow();
You create a brand spanking, unused instance of a new MainWindow. The new keyword is a big clue here. This is not the same as the instance that started the timer. It's like cars: if you put your mobile in the glove box of your car, then go out and buy a new vehicle, would you expect to find your mobile in it's glove box? No - it's in your old car, not the new!
So when you then do this:
C#
ob.Stop_time();
The timer you are stopping is in a different instance of MainWindow (in the glovebox of a different car) so stopping the timer does nothing useful - it was never started in the first place!
To do that, you would need to access the actual instance of MainWindow that started the timer and (presumably) created and displayed the Window1.

And that leads us neatly to the second bad idea! What you are trying to do ties the two windows together - if it worked, you couldn't make any changes to MainWindow without considering if it will impact Window1 and vice versa. That's bad. It's bad from a separation of concerns point of view, from an OOPs point of view, from a reusability point of view, and from a general being-able-to-maintain-the-sucker point of view. Don't do it! Never access controls or anything else on another form except via a property, and never assume that the window that created you is of a specific type.
Instead, create an event in Window1 that MainWindow handles, and it stops the timer itself.
That sounds difficult, but it isn't, not really - it actually very, very simple to do.
Have a look here: Transferring information between two forms, Part 2: Child to Parent[^] it's WinForms, not WPF, but the technique (and even the code) is the same - you just don't need the property this time.
 
Share this answer
 
Comments
Member 12355239 14-Mar-16 15:39pm    
Thanks!
Matt T Heffron 14-Mar-16 17:13pm    
+5

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