C# Program to set the timer to zero


To set the timer information to zero, use the Stopwatch Restart method.

Firstly, begin the Stopwatch −

Stopwatch s = Stopwatch.StartNew();

Then, use Restart() to set the timer to zero −

s.Restart();

Let us see the complete code −

Example

Live Demo

using System;
using System.Threading;
using System.Diagnostics;
public class Demo {
   public static void Main() {
      Stopwatch s = Stopwatch.StartNew();
      Thread.Sleep(500);
      // restart
      s.Restart();
      // begin again
      Thread.Sleep(500);
      Console.WriteLine(s.Elapsed);
   }
}

Output

00:00:00.5004937

Updated on: 22-Jun-2020

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements