Дан следующий код
Код | using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication39 { public class MessageArrivedEventArgs : EventArgs { private string message; public string Message { get { return message; } } public MessageArrivedEventArgs() { message = "No message sent."; } public MessageArrivedEventArgs(string newMessage) { message = newMessage; }
} }
|
Код | using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication39 { public class Display { public void DisplayMessage(object source, EventArgs e) { if (source is Connection) { Console.WriteLine("Message arrived from: {0}", ((Connection)source).Name);
Console.WriteLine("Message Text: {0}", ((MessageArrivedEventArgs)e).Message); } }
} }
|
Код | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Timers;
namespace ConsoleApplication39 { public delegate void MessageHandler(object source, EventArgs e);
public class Connection { public event MessageHandler MessageArrived;
private string name; public string Name { get { return name; } set { name = value; } }
private Timer pollTimer; public Connection() { pollTimer = new Timer(100); pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage); } public void Connect() { pollTimer.Start(); } public void Disconnect() { pollTimer.Stop(); } private static Random random = new Random(); private void CheckForMessage(object source, ElapsedEventArgs e) { Console.WriteLine("Checking for new messages."); if ((random.Next(9) == 0) && (MessageArrived != null)) { MessageArrived(this, new MessageArrivedEventArgs("Hello Mum! ")); } } } }
|
Код | using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication39 { class Program { static void Main(string[] args) { Connection myConnection1 = new Connection(); myConnection1.Name = "First connection."; Connection myConnection2 = new Connection(); myConnection2.Name = "Second connection.";
Display myDisplay = new Display(); myConnection1.MessageArrived += new MessageHandler(myDisplay.DisplayMessage); myConnection2.MessageArrived += new MessageHandler(myDisplay.DisplayMessage); myConnection1.Connect(); myConnection2.Connect(); Console.ReadKey(); }
} }
|
И дано вот такое задание
Цитата | Напишите с использованием универсального синтаксиса (object sender, EventArgs e) код обработчика событий, способного принимать от приведенного ранее в этой главе кода либо событие Timer .Elapsed, либо событие Connection.MessageArrived. Этот обработчик должен выводить на экран строку, сообщающую о том, событие какого типа было получено, вместе со свойством Message параметра MessageArrivedEventArgs или свойством SignalTime параметра ElapsedEventArgs в зависимости от того, какое событие произошло.
|
Насколько я понял, нужно в конструкторе класса Conneсtion к событию Timers.Elapsed привязать в качестве обработчика событий метод DisplayMessage класса Display копировать реализацию метода CheckForMessage, удалить его из класса Connection за ненадобностью, а его скопированную реализацию вставить в метод DisplayMessage класса Display, один и тот же метод обрабатывает оба события, нужно еще задать условия при которых выполнялся нужный участок метода,
ну да еще получается рекурсия
Вот примерно как видеться решение , но чего то не хватает,уважаемые программисты, надеюсь на помощь |