.NET ez 2011-11-30
如果寫Console模式,就會發現只有一個Thread無法同時接收及輸出。
此時只要運用 Thread 達到 顯示 及 接收ESC!
原理就是多建立一個Thread執行程式用,另外一個監測ESC事件用。
using System;
using System.Threading;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//產生Thread輸出數字1,2,3....
Thread t = new Thread(() =>
{
int x = 0;
while (++x > 0) Console.WriteLine(x.ToString());
});
t.Start();
//當使用者按下ESC停止Thread
while (Console.ReadKey(true).Key != ConsoleKey.Escape) ;
t.Abort();
}
}
}
標籤: .NET
