Clear the console every 30s

Asked

Viewed 296 times

1

How do I clean the console every 30s every time? I tried to create with timer but it didn’t work, or I must have done wrong. Follows the code:

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

public static void LimparConsole (object sender){

Console.Clear();
}
  • Which timer is that it? Something else, usually, timer has to be fired, maybe it’s missing a timer.Start()

  • Threading, does not contain timer.start ;/

  • Any reason to be of Threading lib? It could not be itself System timer?

  • I use some thread.Sleep in the class

1 answer

3


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

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

After Thread is 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 range of 10,000 ms only to exemplify and make the test faster. Just change the Thread.Sleep(10000); for the desired range.

  • It would be like I keep moving the console?

  • 1

    after the t.Start() can do the normal process of your application... what is below it is only didactic to represent the application running

Browser other questions tagged

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