Clear the console every 30s

1

How do I clean the console every 30s every time? I tried creating it with timer but it did not work, or I must have done it wrong. Here is the code:

    var time = new Timer(LimparConsole,null,0,30000);

public static void LimparConsole (object sender){

Console.Clear();
}
    
asked by anonymous 21.11.2017 / 18:59

1 answer

3

As the console runs in the Main method, which in turn is a static method, I believe the output is using another Thread.

In the code below, I create a Thread "t" that will wait 10 seconds, and then clean the console.

After the thread has been created, it follows normal system processing.

class Program
{
    static int loop = 0;
    static void Main(string[] args)
    {

        bool aplicacaoRodando = true;

       Thread t = new Thread(()=>{

           while (aplicacaoRodando)
           {
               Thread.Sleep(10000);
               Console.Clear();
               loop = 0;
           }

        });

       t.Start();

       while (aplicacaoRodando)
       {
           Thread.Sleep(1000);
           loop++;
           Console.WriteLine("Tá rodando a aplicação..." + loop);
       }

    }
}

ps. I put the 10,000-ms range just to exemplify and make the test faster. Just change the Thread.Sleep(10000); to the desired interval.

    
21.11.2017 / 19:12