Events in C#


public class Messenger
{
public delegate void MessageEventHandler(object source, MessageEvent e);

public event MessageEventHandler changed;

public void Run()
{
fireMessageEvent("Message");
}

private void fireMessageEvent(string text)
{
log.Info(text);
if (changed != null)
{
changed(this, new MessageEvent(text));
}
}
}

public class MessageEvent : EventArgs
{

private readonly string message;

public MessageEvent(string message)
{
this.message = message;
}

public string Message
{
get { return this.message; }
}
}

public class Client
{
Messenger messenger = new Messenger();

public Client(){
messenger.changed += new Messenger.MessengerEventHandler(messenger_changed);
}

void messenger_changed(object source, MessageEvent e)
{
Console.WriteLine(e.Message);
}
}

Comments

Popular posts from this blog

Parse XML to dynamic object in C#

Tibco Rendezvous (tibrv) C# .Net example