一個事件的發生需要有觸發條件,如定時發生,鼠標點擊等,以及即將發生的事件本身,因此,events相關的最簡單的示例如下:
// 時間觸發器
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();
}
}