Basic concepts of Events-Beginning Visual C #,
A trigger condition is required for an event, such as timed occurrence, Mouse clicking, and the event itself. Therefore, the simplest events-related example is as follows:
// Time trigger class TimerTrigger {private Timer myTimer; public TimerTrigger () {myTimer = new Timer (100);} public TimerTrigger (int timeInterval) {myTimer = new Timer (timeInterval);} // Add the handler public void AddEventHander (ElapsedEventHandler source) with event to the time trigger {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. "; // The Event static void WriteChar (object source, ElapsedEventArgs e) to be added to the time trigger {Console. write (displayString [counter ++ % displayString. length]);} public static void Main () {TimerTrigger timerTrigger = new TimerTrigger (100); timerTrigger. addEventHander (new ElapsedEventHandler (WriteChar); timerTrigger. start (); Console. readLine ();}}
In the main function in the preceding example, the c # ElapsedEventHandler delegated type is directly used. How can I customize the event processing function? The details are as follows:
// Custom time processing stands for 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 ();}}
In the preceding example, the function used to pass a message to an event isWriteFirstMessage(string message)
The passed parameter isstring
Type. Consider that<Timer>.ElapsedEventHandler
The passed parameters of the function are as follows:(object source, ElapsedEventArgs e)
Two parameters, can we implement a similar form? This is acceptable, as shown below:
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(); }}