Question

I am attempting to build a simple stopwatch WPF application.

Here is my code:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Diagnostics;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    public stopWatch = new Stopwatch();

    private void startTimer()
    {
        stopWatch.Start();
        Dispatcher.BeginInvoke(DispatcherPriority.Render, new ThreadStart(ShowElapsedTime));
    }
    void ShowElapsedTime()
    {
        TimeSpan ts = stopWatch.Elapsed;
        lblTime.Text = String.Format("{0:00}:{1:00}.{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
    }
}
}

and here

enter image description here

I am using System.Diagnostics but for some reason I cannot access the Stopwatch

and also I can't find System.Diagnostics in this Dialogue:

enter image description here

Why can't I use System.Diagnostics.Stopwatch and why does System.Diagnostics not appear in the references dialog?

Was it helpful?

Solution

You'll need:

Stopwatch stopWatch = new Stopwatch();

You just have (public stopWatch = new StopWatch) which is not how C# objects are created.

stopWatch is the instance, StopWatch is the class definition.

OTHER TIPS

I had a similar problem and turns out I'd typed "StopWatch" instead of "Stopwatch". I hate case sensitivity!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top