using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { EventWaitTest zhangsan = new EventWaitTest("張三"); EventWaitTest lisi = new EventWaitTest("李四"); Thread t1 = new Thread(new ThreadStart(zhangsan.Consume)); Thread t2 = new Thread(new ThreadStart(lisi.Consume)); Thread t3 = new Thread(new ThreadStart(EventWaitTest.Product)); t1.Start(); t2.Start(); t3.Start(); Console.Read(); } } public class EventWaitTest { private string name; //顧客姓名 //private static AutoResetEvent eventWait = new AutoResetEvent(false); private static ManualResetEvent eventWait = new ManualResetEvent(false); private static ManualResetEvent eventOver = new ManualResetEvent(false); public EventWaitTest(string name) { this.name = name; } public static void Product() { Console.WriteLine("服務員:廚師在作菜呢,兩位稍等"); Thread.Sleep(2000); Console.WriteLine("服務員:宮爆雞丁好了"); eventWait.Set(); while (true) { if (eventOver.WaitOne(1000, false)) { Console.WriteLine("服務員:兩位請買單"); eventOver.Reset(); } } } public void Consume() { while (true) { if (eventWait.WaitOne(1000, false)) { Console.WriteLine(this.name + ":開始吃宮爆雞丁"); Thread.Sleep(2000); Console.WriteLine(this.name + ":宮爆雞丁吃光了"); eventWait.Reset(); eventOver.Set(); break; } else { Console.WriteLine(this.name + ":等着上菜無聊先玩會手機遊戲"); } } } } }