using System; using System.Windows.Forms; class RunClass { public static void Main() { UserInputMonitor inputMonitor = new UserInputMonitor(); MessageDisplayer inputProcessor = new MessageDisplayer(inputMonitor); ManagersStaffMonitor mortimer = new ManagersStaffMonitor(inputMonitor); inputMonitor.Run(); } } class UserInputMonitor { public delegate void UserRequest(object objSender, UserRequestEventArgs e); public event UserRequest OnUserRequest; public void Run() { bool bFinished = false; while (!bFinished) { Console.WriteLine("Select preferred option:"); Console.WriteLine(" Request advertisement - hit A then return"); Console.WriteLine(" Request personal message from Mortimer - hit P then return"); Console.WriteLine(" Exit - hit X then return"); string strInput = Console.ReadLine(); char cRead = (strInput == "") ? ' ' : char.ToUpper(strInput[0]); switch (cRead) { case 'X': bFinished = true; break; case 'A': OnUserRequest(this, new UserRequestEventArgs(RequestType.AdRequest)); break; case 'P': OnUserRequest(this, new UserRequestEventArgs(RequestType.PersonalMessageRequest)); break; } } } } enum RequestType { AdRequest, PersonalMessageRequest } class UserRequestEventArgs: EventArgs { private RequestType request; public UserRequestEventArgs(RequestType request) :base() { this.request = request; } public RequestType Request { get { return request; } } } class MessageDisplayer { public MessageDisplayer(UserInputMonitor monitor) { monitor.OnUserRequest += new UserInputMonitor.UserRequest(UserRequestHandler); } protected void UserRequestHandler(object objSender, UserRequestEventArgs e) { switch (e.Request) { case RequestType.AdRequest: Console.WriteLine("Moniter...."); break; case RequestType.PersonalMessageRequest: Console.WriteLine("Personal Message Request"); break; } } } class ManagersStaffMonitor { public ManagersStaffMonitor(UserInputMonitor monitor) { monitor.OnUserRequest += new UserInputMonitor.UserRequest(UserRequestHandler); } protected void UserRequestHandler(object objSender, UserRequestEventArgs e) { if (e.Request == RequestType.PersonalMessageRequest) { MessageBox.Show("Hello, andylin", "Monitor"); } } }
C#的事件
最新推荐文章于 2024-11-04 13:18:03 发布