Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find reference to System.Diagnostics.Stopwatch

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?

like image 440
jth41 Avatar asked Aug 30 '13 13:08

jth41


People also ask

What is System diagnostics stopwatch?

A Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals. In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.

How to add Stopwatch in c#?

To use it, you have to create a new instance of Stopwatch() and tell it when to stop and start. Stopwatch timer = new Stopwatch(); timer. Start(); // insert some code to execute here timer. Stop(); Console.

How accurate is C# stopwatch?

Stopwatch class does accurately measure time elapsed, but the way that the ElapsedTicks method works has led some people to the conclusion that it is not accurate, when they really just have a logic error in their code.

What is Stopwatch C#?

Stopwatch is a class in C# to measure the elapsed time. Use it to calculate the time a function took to execute. It is found under System. Diagnostics.


1 Answers

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.

like image 155
Darren Avatar answered Sep 18 '22 22:09

Darren