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?
– user92401
@Betadarknight is enough that the interval is 3600 seconds, ie
intervalo = TimeSpan.FromSeconds(3600);
– Rovann Linhalis