using System;using System.Threading;class Example{ static void Main() { TimeSpan interval = new TimeSpan(0, 0, 2); for (int i = 0; i < 5; i++) { Console.WriteLine("Sleep for 2 seconds."); Thread.Sleep(interval); } Console.WriteLine("Main thread exits."); }}
/* This example produces the following output:Sleep for 2 seconds.Sleep for 2 seconds.Sleep for 2 seconds.Sleep for 2 seconds.Sleep for 2 seconds.Main thread exits. */
每次输出
"Sleep for 2 seconds." 都会停留2秒