我有一個控制檯應用程序,當我使用Console.ReadLine()
,應用程序將顯示「Hello World」的。爲什麼Console.ReadKey()
不能`在Timer中使用Console.WriteLine爲什麼它會出現?
static void Main(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.Elapsed += timer_Elapsed;
timer.Enabled = true;
Console.ReadKey();// When use ReadLine() work fine;
}
static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Hello World");
}
修正:http://support.microsoft.com/kb/2805221
我不知道你在問什麼。這將基本上每秒鐘顯示'Hello World',直到您按下一個鍵。你想要什麼? – Jonesopolis
如果您的計算機上安裝了.NET 4.5,請確保您已啓用Windows Update。您將得到一個更新,該更新修復了ReadKey()帶來阻止WriteLine()將文本寫入控制檯的鎖的問題。 –
@HansPassant是的,你是對的!謝謝。 – Ruan