Chronometer with milliseconds in c #

0

I have this code in my timer. What happens is that when making comparisons with a chronometer I do not have the required accuracy, I have no idea what the error may be. I need to make a chronometer to mark me m-s-ms.

 private void t1_Tick(object sender, EventArgs e)
    {
        t1.Interval = 10;
        ms++;
        if (ms == 100)
        {
            s++;
            ms = 0;
        }
        else if (s == 60)
        {
            m++;
            s = 0;
        }
        Step1.Text = m.ToString().PadLeft(2, '0') + ":" + s.ToString().PadLeft(2, '0') + ":" + ms.ToString().PadLeft(2, '0');
    }
    
asked by R. Quiñonez 13.06.2017 в 02:43
source

3 answers

1

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.

    
answered by 13.06.2017 / 16:12
source
2

First you create an instance of class StopWatch :

Stopwatch stopwatch = new Stopwatch();

Then you start it when you're interested:

stopwatch.Start();

Finally to obtain the time in minutes and seconds we format it in the following way:

TimeSpan ts = stopwatch.Elapsed;
ts.ToString("mm\:ss\.ff");
    
answered by 13.06.2017 в 08:41
0

Try with TimeSpan:

TimeSpan start = DateTime.Now.TimeOfDay;

// proceso

TimeSpan end = DateTime.Now.TimeOfDay;

TimeSpan time = end - start;

Console.WriteLine("{0}-{1}-{2}", time.Minutes, time.Seconds, time.Milliseconds);
    
answered by 13.06.2017 в 03:16