错误 1 “DisplayMessage”的重载均与委托“Ch13Ex03.MessageHandler”不匹配?????????????代码
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.MessageArried += new MessageHandler(myDisplay.DisplayMessage);错误的地方
myConnection1.Connect();
Console.ReadKey();
}
}
//定义委托
public delegate void MessageHandler(Connection source,MessageArriedEventArgs e);
public class Connection
{
//定义事件
public event MessageHandler MessageArried;
private string name;
public string Name
{
get
{
return name;
}
set;
}
//计时器
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 Random random = new Random();
private void CheckForMessage(object source, ElapsedEventArgs e)
{
Console.WriteLine("Checking for new message!");
if ((random.Next(9) == 0) &&( MessageArried != null))
{
MessageArried(this,new MessageArriedEventArgs("Hello Mum!"));
}
}
}