# 使用Event處理排隊機制 C# ```C#= using System; using System.Collections.Generic; using System.Threading; namespace ConsoleApp2 { public class WorkArgs : EventArgs { public bool IsChairEmpty = true; } } ``` comment ```C#= class Program { static Queue<string> CustomerQueue = new Queue<string>(); static bool isOpening = true; static EventHandler<WorkArgs> StaffReported; static Thread staff = new Thread(Work); static string CustomerInChair = null; static object chair = new object(); static void Main(string[] args) { CustomerQueue.Enqueue("A"); CustomerQueue.Enqueue("B"); CustomerQueue.Enqueue("C"); CustomerQueue.Enqueue("D"); CustomerQueue.Enqueue("E"); Program.StaffReported += AssignNextWork; isOpening = true; staff.Start(); while (true) { Console.WriteLine("Enter customer name for queuing or \"close\" to close services: ") ; string line = Console.ReadLine(); if (line == "close") { isOpening = false; break; } else { CustomerQueue.Enqueue(line); } } while (CustomerQueue.Count != 0) ; } static void AssignNextWork(object sender, WorkArgs args) { if (CustomerQueue.Count != 0) { CustomerInChair = CustomerQueue.Dequeue(); } } static void Work() { int id = Thread.CurrentThread.ManagedThreadId; CustomerInChair = null; do { if (StaffReported != null) { WorkArgs args = new WorkArgs(); EventHandler<WorkArgs> staffReported = StaffReported; staffReported(null, args); } if (CustomerInChair != null) Console.WriteLine(" Thread {0} serve {1} now", id, CustomerInChair); else Console.WriteLine(" Chair is empty"); Thread.Sleep(2000); CustomerInChair = null; } while (isOpening || CustomerQueue.Count != 0); } } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up