The following code starts the timer (the behavior is encapsulated in a class) and stops it by pressing enter.
Each one millisecond shows the elapsed time per screen.
At the end it returns the total elapsed time:
using System;
using System.Timers;
namespace ConsoleTimer
{
class Program
{
static void Main(string[] args)
{
double intervalo = 1;
Cronometro cron = new Cronometro(intervalo);
cron.Iniciar();
System.Console.ReadKey();
TimeSpan final = cron.Detener();
System.Console.WriteLine("Transcurridos: " + final.ToString());
System.Console.ReadKey();
}
}
public class Cronometro : Timer
{
private DateTime _inicio;
/// <summary>
/// Constructor
/// </summary>
/// <param name="intervalo">En milisegundos</param>
public Cronometro(double intervalo)
{
base.Interval = intervalo;
this.Elapsed += Tic;
}
public void Iniciar()
{
this._inicio = DateTime.Now;
this.Start();
}
public TimeSpan Detener()
{
this.Stop();
TimeSpan transcurrio = DateTime.Now - this._inicio;
return transcurrio;
}
private void Tic(object sender, ElapsedEventArgs e)
{
System.Console.Clear();
TimeSpan transcurrio = DateTime.Now - this._inicio;
System.Console.WriteLine(transcurrio.ToString());
}
}
}
I hope it serves as an example. The votes are appreciated.