Timer telling you when it’s over

Asked

Viewed 252 times

2

How can I make the timer tell you when it’s running out on the console? It cleans the console every 1 hour

Example:

> Falta 3 segundos para o terminar
> Falta 2 segundos para o terminar
> Falta 1 segundos para o terminar

Timer:

private static void Timer(Object o)
        {
            Console.Clear();
            GC.Collect();
        }

Timer t = new Timer(Timer, null, 0, 3600000);

2 answers

3


The interval of timer it has to be 1 second, because every second it will check if it is time to clear the console, or if it is <= 5, write on screen 5...4...3...2...1...

In another variable, you save the interval to execute the command Clear, and every second, you decrease it, until it reaches 0, and then you clear the console, and you restore the interval.

I made the following example:

class Program
{
    static TimeSpan intervalo;
    static void Main(string[] args)
    {
        intervalo = TimeSpan.FromSeconds(10);

        Timer t = new Timer(ClearConsole, null, 0, 1000);

        while (true)
        {
            Console.WriteLine("Aplicação rodando...");
            Thread.Sleep(3000);
        }


    }

    private static void ClearConsole(object state)
    {
        if (intervalo.TotalSeconds == 0)
        {
            intervalo = TimeSpan.FromSeconds(10);

            Console.Clear();
        }
        else
        {
            intervalo = intervalo - TimeSpan.FromSeconds(1);
            if (intervalo.TotalSeconds <=5)
            {
                Console.WriteLine("Console será limpo em " + intervalo.TotalSeconds + " segundos...");
            }
        }
    }

}
  • How would it look if I needed the console to clear every 1 hour? I would have to set the console timer for an hour and the interval as well?

  • @Betadarknight is enough that the interval is 3600 seconds, ie intervalo = TimeSpan.FromSeconds(3600);

0

You could do it this way too, maybe it’s not the best but it works:

class Program
    {
        static void Main(string[] args)
        {
            var tempoDuracao = 5; // Em segundos se por 3600 se torna uma hora e assim por diante
            var cronometro = TimeSpan.FromSeconds(tempoDuracao);

            while (tempoDuracao != 0)
            {
                if (tempoDuracao > 3)
                {
                    Console.Clear();
                    Console.WriteLine(cronometro);
                }
                else
                {
                    if (tempoDuracao == 3)
                        Console.Clear();

                    if (tempoDuracao == 1)
                        Console.WriteLine("Falta " + cronometro.Seconds + " segundo para o programa terminar");
                    else
                        Console.WriteLine("Faltam " + cronometro.Seconds + " segundos para o programa terminar");
                }

                cronometro = cronometro.Subtract(TimeSpan.FromSeconds(1));
                Thread.Sleep(1000);
                tempoDuracao--;
            }
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.