-3
public static string stopwatch()
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;
        // Format and display the TimeSpan value.
        String elapsedTime = String.Format("{0:0}.{1:000}", ts.Seconds, ts.Milliseconds / 1000);
        return elapsedTime;
    }

    static void Main(string[] args)
    {
        string elptime = stopwatch();
        for (Console.ReadKey(true); ;)
        {
            Console.WriteLine(elptime);
        }
    }
}

So I want to make a program that will each time i press button write in console value from Stopwatch();with this order, for example:

0.520

0.801

1.255

But instead of this it comes down to:

0.000

0.000

0.000

0.000

0.000

Like infinite array

3
  • 2
    every call to stopwatch starts a new stopwatch instance you should reuse it. Commented Sep 16, 2016 at 18:04
  • also you aren't calling stopwatch multiple times... Commented Sep 16, 2016 at 18:06
  • 1
    You only check the "elapsed time" once and store that result in a variable, which you just print over and over. If you want to get the updated time, you need to get it from the Stopwatch instance again.
    – David
    Commented Sep 16, 2016 at 18:08

3 Answers 3

0

You should use the same instance of StopWatch, after you hit key, timer should tick again. So try this code,

static Stopwatch watch = new Stopwatch();
        static void Main(string[] args)
        {
            while (true)
            {
                startWatch();
                for (Console.ReadKey(true); ;)
                {
                    string elptime = stopwatch();
                    Console.WriteLine(elptime);
                    break; // to exit for and startWatch() again.
                }
            }
        }

        static public void startWatch()
        { 
            watch.Start();
        }
        public static string stopwatch()
        {
            String elapsedTime = "";
            TimeSpan ts = watch.Elapsed;
            elapsedTime = String.Format("{0:0}.{1:000}", ts.Seconds, ts.Milliseconds / 1000);
            return elapsedTime;
        }

Hope helps,

3
  • Works, but milliseconds are not visible. I get numbers like for example: 1.000, 1.000, 2.000, 3.000, 5.000...
    – Mayketi
    Commented Sep 16, 2016 at 20:50
  • Why are you dividing Milliseconds to 1000? @Mayketi Commented Sep 16, 2016 at 21:10
  • Solved, thank you very much!
    – Mayketi
    Commented Sep 17, 2016 at 7:15
0

That happens because every time you call the stopwatch() method it creates a new instance of stopwatch and starts from 0.

A better approach would be to create a Stopwatch instance inside main method, start it before the for loop and call the elapsed time of stopwatch inside the for loop.

A simple code

static void Main(string[] args)
{
    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();
    for (Console.ReadKey(true); ;)
    {
        TimeSpan ts = stopWatch.Elapsed;
        String elapsedTime = String.Format("{0:0}.{1:000}", ts.Seconds, ts.Milliseconds / 1000);
        Console.WriteLine(elapsedTime);
    }
}
0

You will want to define a class with a GetElapsed() method. Call GetElapsed when a key is pressed.

But this starts to beg the question, why wrap the class if you are really just mocking the methods? Ya, just use the class directly as hakanshehu suggests, wrapping it is a waste.

public class MyStopWatch
    {
        Stopwatch stopWatch;
        public MyStopWatch()
        {
          stopWatch = new Stopwatch();
          stopWatch.Start();
        }
        public String GetElapsed()
        {
            TimeSpan ts = stopWatch.Elapsed;
            // Format and display the TimeSpan value.
            return String.Format("{0:0}.{1:000}", ts.Seconds, ts.Milliseconds / 1000);
        }

    }

    static void Main(string[] args)
    {
        MyStopWatch msw = new MyStopWatch();
        for (Console.ReadKey(true); ;)
        {
            Console.WriteLine(msw.GetElapsed());
        }
    }
}
2
  • You can't call stopWatch.Start(); directly at there, that will give error. Commented Sep 16, 2016 at 18:30
  • Updated, thanks @Berkay
    – Adam
    Commented Nov 28, 2017 at 21:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.