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();
    }
}
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
  • Well yeah, I should.. But anyway, it just gives me "Timer has stopped working" and typical Windows crash info, do you mean that? –  Jul 31 '11 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. – Michael Petrotta Jul 31 '11 at 20:54
  • Are you really need to tick each millisecond? – sll Jul 31 '11 at 20:55
  • Well not really.. I know it's not necessary, this is only for testing purposes. –  Jul 31 '11 at 21:01

1 Answers1

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(...
slugster
  • 49,403
  • 14
  • 95
  • 145
  • 2
    @bah, what would you expect to happen if you call `Start` on a timer that hasn't been initialized? – Michael Petrotta Jul 31 '11 at 20:56
  • @bah, i extended my answer for you. – slugster Jul 31 '11 at 20:58
  • 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? –  Jul 31 '11 at 21:09
  • @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. – slugster Jul 31 '11 at 21:25
  • 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. – Eon Jul 31 '11 at 21:28