常建57

路漫漫其修远兮。。。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Here is a sample about delegate and event.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestHeaterDelegateSample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Heater heater = new Heater();
            Monitor monitor = new Monitor();

            //The below can be modified with the below solution1.
            //HeaterEventHander hander = new HeaterEventHander(monitor.OnBoil);
            //heater.HeaterEvent += hander;
            heater.HeaterEvent += new HeaterEventHander(monitor.OnBoil);

            heater.OnHeater();

            Console.ReadLine();
        }
    }

    public delegate void HeaterEventHander(object sender, HeaterEventArgs e); // EventArgs doesn't have real data.

    public class HeaterEventArgs : EventArgs
    {
        private string alertText;
        public string AlertText
        {
            get { return ("The Temperatur is 100 degree now."); }
          //  set { alertText = value; }
        }
    }

    public class Heater 
    {
        private int temperature;
        private string alterText;

        private int Temperature
        {
            get { return temperature; }
            set { temperature = value; }
        }
        private string AlterText 
        {
            get { return alterText; }
            set { alterText = value; }
        }
        public event HeaterEventHander HeaterEvent;
       
        public void OnHeater()
        {
            this.temperature = 0;
            for (;;Temperature++ )
            {
                if (Temperature >= 100)
                {
                    Console.WriteLine("Thie is the event sender.");
                    HeaterEventArgs e = new HeaterEventArgs();
                    HeaterEvent(this, e);
                    break;
                    // Raise the water has boiled.
                }
            }
        }
    }

    public class Monitor
    {
        public void OnBoil(object sender, HeaterEventArgs e)
        {
            Console.WriteLine("The sender data is: "+e.AlertText);
            Console.WriteLine("The water has boiled. And we have monitored it.");
        }
    }
}

 And the below is the msdn sample. It's more perfect and ofcourse more difficult.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestClockAlarmSample
{
    // Class that contains the data for
    // the alarm event. Derives from System.EventArgs.
    //
    public class AlarmEventArgs : EventArgs
    {
        private bool snoozePressed;
        private int nrings;

        //Constructor.
        //
        public AlarmEventArgs(bool snoozePressed, int nrings)
        {
            this.snoozePressed = snoozePressed;
            this.nrings = nrings;
        }

        // The NumRings property returns the number of rings
        // that the alarm clock has sounded when the alarm event
        // is generated.
        //
        public int NumRings
        {
            get { return nrings; }
        }

        // The SnoozePressed property indicates whether the snooze
        // button is pressed on the alarm when the alarm event is generated.
        //
        public bool SnoozePressed
        {
            get { return snoozePressed; }
        }

        // The AlarmText property that contains the wake-up message.
        //
        public string AlarmText
        {
            get
            {
                if (snoozePressed)
                {
                    return ("Wake Up!!! Snooze time is over.");
                }
                else
                {
                    return ("Wake Up!");
                }
            }
        }
    }

    // Delegate declaration.
    //
    public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);

    // The Alarm class that raises the alarm event.
    //
    public class AlarmClock
    {
        private bool snoozePressed = false;
        private int nrings = 0;
        private bool stop = false;

        // The Stop property indicates whether the
        // alarm should be turned off.
        //
        public bool Stop
        {
            get { return stop; }
            set { stop = value; }
        }

        // The SnoozePressed property indicates whether the snooze
        // button is pressed on the alarm when the alarm event is generated.
        //
        public bool SnoozePressed
        {
            get { return snoozePressed; }
            set { snoozePressed = value; }
        }

        // The event member that is of type AlarmEventHandler.
        //
        public event AlarmEventHandler Alarm;

        // The protected OnAlarm method raises the event by invoking
        // the delegates. The sender is always this, the current instance
        // of the class.
        //
        protected virtual void OnAlarm(AlarmEventArgs e)
        {
            AlarmEventHandler handler = Alarm;
            if (handler != null)
            {
                // Invokes the delegates.
                handler(this, e);
            }
        }

        // This alarm clock does not have
        // a user interface.
        // To simulate the alarm mechanism it has a loop
        // that raises the alarm event at every iteration
        // with a time delay of 300 milliseconds,
        // if snooze is not pressed. If snooze is pressed,
        // the time delay is 1000 milliseconds.
        //
        public void Start()
        {
            for (; ; )
            {
                nrings++;
                if (stop)
                {
                    break;
                }
                else
                {
                    if (snoozePressed)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(300);
                    }
                    AlarmEventArgs e = new AlarmEventArgs(snoozePressed, nrings);
                    OnAlarm(e);
                }
            }
        }
    }

    // The WakeMeUp class has a method AlarmRang that handles the
    // alarm event.
    //
    public class WakeMeUp
    {
        public void AlarmRang(object sender, AlarmEventArgs e)
        {
            Console.WriteLine(e.AlarmText + "\n");

            if (!(e.SnoozePressed))
            {
                if (e.NumRings % 10 == 0)
                {
                    Console.WriteLine(" Let alarm ring? Enter Y");
                    Console.WriteLine(" Press Snooze? Enter N");
                    Console.WriteLine(" Stop Alarm? Enter Q");
                    String input = Console.ReadLine();

                    if (input.Equals("Y") || input.Equals("y"))
                    {
                        return;
                    }
                    else if (input.Equals("N") || input.Equals("n"))
                    {
                        ((AlarmClock)sender).SnoozePressed = true;
                        return;
                    }
                    else
                    {
                        ((AlarmClock)sender).Stop = true;
                        return;
                    }
                }
            }
            else
            {
                Console.WriteLine(" Let alarm ring? Enter Y");
                Console.WriteLine(" Stop Alarm? Enter Q");
                String input = Console.ReadLine();
                if (input.Equals("Y") || input.Equals("y"))
                {
                    return;
                }
                else
                {
                    ((AlarmClock)sender).Stop = true;
                    return;
                }
            }
        }
    }


    // The driver class that hooks up the event handling method of
    // WakeMeUp to the alarm event of an Alarm object using a delegate.
    // In a forms-based application, the driver class is the
    // form.
    //
    public class AlarmDriver
    {
        public static void Main(string[] args)
        {
            // Instantiates the event receiver.
            WakeMeUp w = new WakeMeUp();

            // Instantiates the event source.
            AlarmClock clock = new AlarmClock();

            // Wires the AlarmRang method to the Alarm event.
            clock.Alarm += new AlarmEventHandler(w.AlarmRang);

            clock.Start();
        }
    }
}
View Code

 

posted on 2013-12-25 22:12  常建57  阅读(178)  评论(0编辑  收藏  举报