一个事件的发生需要有触发条件,如定时发生,鼠标点击等,以及即将发生的事件本身,因此,events相关的最简单的示例如下:
class="sourceCode cs">// 时间触发器
class TimerTrigger
{
private Timer myTimer;
public TimerTrigger()
{
myTimer = new Timer(100);
}
public TimerTrigger(int timeInterval)
{
myTimer = new Timer(timeInterval);
}
// 向时间触发器添加具事件的handler
public void AddEventHander(ElapsedEventHandler source)
{
myTimer.Elapsed += source;
}
public void Start()
{
myTimer.Start();
}
public void Stop()
{
myTimer.Stop();
}
}
class Program
{
static int counter = 0;
static string displayString =
"This string will appear one letter at a time. ";
// 即将被添加到时间触发器的事件
static void WriteChar(object source, ElapsedEventArgs e)
{
Console.Write(displayString[counter++ % displayString.Length]);
}
public static void Main()
{
TimerTrigger timerTrigger = new TimerTrigger(100);
timerTrigger.AddEventHander(new ElapsedEventHandler(WriteChar));
timerTrigger.Start();
Console.ReadLine();
}
}
在上面的例子的main函数中,直接使用了c#自带的ElapsedEventHandler delegated类型,那么如何自定义事件处理函数呢?具体如下:
// 自定义时间处理代表
public delegate void MessageHandler(string message);
class TimerTrigger
{
private Timer myTimer;
private event MessageHandler MessageArrived;
public TimerTrigger(int timeInterval)
{
myTimer = new Timer(timeInterval);
myTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
}
public void AddEventHandler(MessageHandler source)
{
MessageArrived += source;
}
public void Start()
{
myTimer.Start();
}
public void Stop()
{
myTimer.Stop();
}
private void CheckForMessage(object source, ElapsedEventArgs e)
{
Console.WriteLine("Checking for new messages.");
if (MessageArrived != null)
{
MessageArrived("hello world");
}
}
}
class Program
{
static int counter = 0;
static void WriteFirstMessage(string message)
{
Console.WriteLine("The First {0} number: {1}", message, counter++);
}
static void WriteSecondMessage(string message)
{
Console.WriteLine("The Second {0} number: {1}", message, counter++);
}
public static void Main()
{
TimerTrigger timerTrigger = new TimerTrigger(100);
timerTrigger.AddEventHandler(new MessageHandler(WriteFirstMessage));
timerTrigger.AddEventHandler(new MessageHandler(WriteSecondMessage));
timerTrigger.Start();
Console.ReadLine();
}
}
在上述的例子中,将消息传递给处理事件所用的函数为WriteFirstMessage(string message)
,其传递的参数为string
类型的。再考虑道<Timer>.ElapsedEventHandler
所代表的函数的传递参数为:(object source, ElapsedEventArgs e)
两个参数,那么我们能否实现类似的形式呢?这是可以的,如下:
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;
}
}
public delegate void MessageHandler(TimerTrigger source,
MessageArrivedEventArgs e);
public class TimerTrigger
{
private Timer myTimer;
private event MessageHandler MessageArrived;
public TimerTrigger(int timeInterval)
{
myTimer = new Timer(timeInterval);
myTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
}
public void AddEventHandler(MessageHandler source)
{
MessageArrived += source;
}
public void Start()
{
myTimer.Start();
}
public void Stop()
{
myTimer.Stop();
}
private void CheckForMessage(object source, ElapsedEventArgs e)
{
Console.WriteLine("Checking for new messages.");
if (MessageArrived != null)
{
MessageArrived(this, new MessageArrivedEventArgs("hello world"));
}
}
}
class Program
{
static int counter = 0;
static void WriteFirstMessage(TimerTrigger source, MessageArrivedEventArgs e)
{
Console.WriteLine("The First {0} number: {1}", e.Message, counter++);
}
static void WriteSecondMessage(TimerTrigger source, MessageArrivedEventArgs e)
{
Console.WriteLine("The Second {0} number: {1}", e.Message, counter++);
}
public static void Main()
{
TimerTrigger timerTrigger = new TimerTrigger(100);
timerTrigger.AddEventHandler(new MessageHandler(WriteFirstMessage));
timerTrigger.AddEventHandler(new MessageHandler(WriteSecondMessage));
timerTrigger.Start();
Console.ReadLine();
}
}