3

I'm trying to make a simple stopwatch but it just doesn't work.. The app just crashes when I press the buttons. What's wrong?

public partial class MainWindow : Window
{
    private DispatcherTimer timer;

    public MainWindow()
    {
        InitializeComponent();
        DispatcherTimer timer = new DispatcherTimer(new TimeSpan(0, 0, 0, 0, 1), DispatcherPriority.Normal, delegate
        {
            this.Show.Text = DateTime.Now.ToString("HH:mm:ss:fff");
        }, this.Dispatcher);
    }

    private void Start(object sender, RoutedEventArgs e)
    {
        timer.Start();
    }

    private void Stop(object sender, RoutedEventArgs e)
    {
            timer.Stop();
    }
}
4
  • Well yeah, I should.. But anyway, it just gives me "Timer has stopped working" and typical Windows crash info, do you mean that? Commented Jul 31, 2011 at 20:52
  • 1
    Catch all exceptions thrown, before Windows does and terminates the app. The exception stack trace will tell you everything you need to know. Commented Jul 31, 2011 at 20:54
  • Are you really need to tick each millisecond? Commented Jul 31, 2011 at 20:55
  • Well not really.. I know it's not necessary, this is only for testing purposes. Commented Jul 31, 2011 at 21:01

1 Answer 1

4

Your problem is this:

DispatcherTimer timer = ...

you have created a new instance of the timer which is scoped to your constructor. You have not set the member variable timer. This means when you hit the start button you will be trying to start a timer that has not been instantiated yet and you will get a NullReferenceException. I suggest you:

  • rename the member variable timer to _timer. This helps avoid confusion to similarly named local variables.
  • change the line DispatcherTimer timer = new DispatcherTimer to _timer = new DispatcherTimer(...
Sign up to request clarification or add additional context in comments.

5 Comments

@bah, what would you expect to happen if you call Start on a timer that hasn't been initialized?
@bah, i extended my answer for you.
but I still don't get it, if I have a class variable timer, why does it create local variable with that exact name? Is this normal?
@bah - yes this is normal, both instances of the variable are available to you from within the constructor (just use this.timer to access the class variable while also having the local one). This is one reason why i detest it when developers don't use some standard for naming their class/member variables, it leads to confusion just like this. It can be avoided by having good coding habits, although there are also some IDE plugins (like ReSharper) that will help catch this sort of thing.
I have also taken a swing at his problem, and tried to write a solution of my own (awaiting downvotes now for some reason), but I remember when I did this once , I was also plagued by nullReference exceptions. I felt I needed to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.