Sreginogemoh
Sreginogemoh

Reputation: 1309

WPF field is null

I have private field

private DateTime? StartTime;

I am setting that field on button click

private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            StartTime = DateTime.Now;
        }

But when i am accessing that field from EventHandler

private void InitTimers()
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();

            dispatcherTimer.Tick += DispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            lbTime.Content = DateTime.Now.ToString("HH:mm:ss");
            lbTodayDate.Content = DateTime.Now.ToString("MM/dd/yyyy");

            if(StartTime != null) //Always null
                lbElipsedTime.Content = DateTime.Now - StartTime;

            CommandManager.InvalidateRequerySuggested();
        }

StartTime always remains null

May be someone could explain that?

InitTimmers getting called inside public MainWindow() default constructor when program starts.

Button click fires, timer ticking, I've checked that. On button click StartTime initialized to DateTime.Now, so it is not equal to null, but on timer tick it is equal to null. Just for test i set StartTime inside intimidate window so it is became not null and expression if(StartTime != null) not false any more

UPDATE

Ok I've done something stupid, i am running asynchronous method, the key is there:

 private void btnStart_Click(object sender, RoutedEventArgs e)
            {
                StartTime = DateTime.Now;
                DoAsyncMethod(); //should do StartTime = null inside.
                StartTime = null
            }

I should do StartTime = null inside async method, that is it, thank you guys.

Upvotes: 0

Views: 196

Answers (2)

Eren Ersönmez
Eren Ersönmez

Reputation: 39085

I've just used this code in a test app and it works OK. Adding the code here for your comparison:

enter image description here

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DateTime? StartTime;

        public MainWindow()
        {
            InitTimers(); 
            InitializeComponent();
        }


        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            StartTime = DateTime.Now;
        }

        private void InitTimers()
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();

            dispatcherTimer.Tick += DispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (StartTime != null) //Always null
                lbElipsedTime.Content = DateTime.Now - StartTime;

            CommandManager.InvalidateRequerySuggested();
        }
    }
}

Upvotes: 3

Sheridan
Sheridan

Reputation: 69959

If you put a breakpoint in the DispatcherTimer_Tick and the btnStart_Click handlers, you will probably find that the DispatcherTimer_Tick handler is called first... therefore the StartTime property would not have been set yet.

If you're saying that even after consecutive Button clicks, the value is still null, then either you are setting it to null somewhere else, or your btnStart_Click handler is not called... are you sure that it is wired up correctly?

Upvotes: 0

Related Questions