-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

Community
  • 1
  • 1
Mayketi
  • 25
  • 1
  • 6

3 Answers3

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());
        }
    }
}
Adam
  • 119
  • 6
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);
    }
}
hakanshehu
  • 1
  • 1
  • 1
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,

Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37