static void Main(string[] args) { var stopWatch = new StopWatch(); Console.WriteLine("Type 1 to Start. n/ Type 0 to Stop."); while (true) { switch (Console.ReadLine()) { case "1": stopWatch.Start(); break; case "0": stopWatch.Stop(); Console.WriteLine("Duration was: " + stopWatch.GetInterval().Seconds + "s"); break; default: Console.WriteLine("Make sure you Type the correct Key!"); Console.WriteLine("Type 1 to Start. n/ Type 0 to Stop."); break; } } }
static void Main(string[] args) { var stopwatch = new StopWatch(); for (var i = 0; i < 2; i++) { stopwatch.Start(); Thread.Sleep(1000); //stopwatch.Start(DateTime.Now); stopwatch.Stop(); Console.WriteLine("Duration: " + stopwatch.GetInterval()); Console.WriteLine("Press Enter to run the stopwatch one more time."); Console.ReadLine(); } }
static void StartStopWatch() { StopWatch stopWatch = new StopWatch(); Console.WriteLine("Start_Time : {0}", DateTime.Now); stopWatch.Start(); Console.WriteLine("Press enter to stop."); Console.ReadKey(); stopWatch.Stop(); Console.WriteLine("Stop_Time : {0}", DateTime.Now); Console.WriteLine("Duration: {0}", stopWatch.GetInterval()); Console.WriteLine("Do you want to restart the stopwatch? y/n"); string request = Console.ReadLine(); if (request == "y" || request == "yes") { StartStopWatch(); } Console.WriteLine("Thanks for using RyStopWatch"); }
static void Stopwatch() { var timer = new StopWatch(); for (var i = 0; i < 2; i++) { timer.Start(); Thread.Sleep(1000); timer.Stop(); Console.WriteLine("Duration: " + timer.GetInterval()); if (i < 1) { Console.WriteLine("Press enter to run the stopwatch again."); } else { Console.WriteLine("Press enter to quit"); } Console.ReadLine(); } }