When I first compiled and executed this code segment (excluding the commented statements), it seemed that the loop didn’t wait for 1 millisecond (which usually takes about ten to twenty milliseconds) before executing other code. Because the code I use to do other things depends on waiting for a millisecond (usually ten to twenty milliseconds) on the first iteration of the loop, it resulted in an error. Then, I modified the code slightly (adding the commented statements), and subsequent executions were all normal. Why did this happen?
Here’s the code snippet for reference:
for (int i = 0; i < 10; i++) {
if (i == 0) {
//Console.WriteLine(Stopwatch.GetTimestamp());
Thread.Sleep(1);
} else {
Console.ReadLine();
}
//Console.WriteLine(Stopwatch.GetTimestamp());
// Do other things
}