Basic concepts of Events-Beginning Visual C #,

Source: Internet
Author: User

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 isstringType. Consider that<Timer>.ElapsedEventHandlerThe 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();    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.