Reputation: 3
I'm pretty new to programming, I've started with C# and I'm doing a project where there is a race, basically just need to randomize the position of cars in a list, but i wanted to make it so the console updates the progress of the cars, instead of just showing initial and final position, I tried to just randomize position, show in console, clear console in a for loop with a count of 5, the problem is that it goes too fast, is there any way to make the loop only loop after a certain time to make sure it is possible to see the order of the cars change in the console? Here's what I tried (corrida1 is a class that consists of a list of cars and the method posicaoAleatoria, which randomizes the list, corrida1.corrida is the list itself):
for (int i = 0; i < 5; i++)
{
corrida1.posicaoAleatoria();
foreach (var carro in corrida1.corrida)
{
Console.WriteLine("{0} conduzindo o carro número {1} de cores {2}, {3} e {4}", carro.nomePiloto, carro.numero, carro.cores, carro.marca, carro.modelo);
}
Console.Clear();
}
Upvotes: 0
Views: 73
Reputation: 94
I changed the code as needed. I hope I understood correctly.
for (int i = 0; i < 5; i++)
{
Thread.Sleep (50);
corrida1.posicaoAleatoria();
foreach (var carro in corrida1.corrida)
{
Thread.Sleep (50);
Console.WriteLine("{0} conduzindo o carro número {1} de cores {2}, {3} e {4}", carro.nomePiloto, carro.numero, carro.cores, carro.marca, carro.modelo);
}
Console.Clear();
Console.ReadKey()
}
You can change the value to 50 as needed
Upvotes: 0
Reputation: 572
Use Thread.Sleep()
. The argument for it is in milliseconds. So if you want to pause the code for say, 1 minute, it will be as follows:
Thread.Sleep(1 * 60 * 1000);
where 1 represents the minute, 60 the seconds in a minute and 1000 number of milliseconds in a second.
Upvotes: 2
Reputation: 1193
You need Thread.Sleep or Console.Read.
Console.Read will stop the process until you enter something.
Upvotes: 1