c#事件

来源:互联网 发布:qq三国79js 79xs 编辑:程序博客网 时间:2024/05/29 19:43
using System;using System.IO;namespace BoilerEventApp1{    class Boiler    {        private int temp;        private int pressure;        public Boiler(int t,int p)        {            temp = t;            pressure = p;        }        public int getTemp()        {            return temp;        }        public int getPressure()        {            return pressure;        }    }    class DelegateBoilerEvent    {        public delegate void BoilerLogHandler(string status);        public event BoilerLogHandler BoilerEventLog;        public void LogProcess()        {            string remarks = "0.K";            Boiler b = new Boiler(100, 12);            int t = b.getTemp();            int p = b.getPressure();            if(t>150||t<80||p<12||p>15)            {                remarks = "Nedd Maintenance";            }            OnBilerEventLog("Logging Info:\n");            OnBilerEventLog("Temparature:" + "\t" + t + "\nPressure:" + p);            OnBilerEventLog("\nMessage:" + remarks);        }                protected void OnBilerEventLog(string message)        {            if(BoilerEventLog!=null)            {                BoilerEventLog(message);            }        }    }    class BoilerInfoLogger    {        FileStream fs;        StreamWriter sw;        public BoilerInfoLogger(string filename)        {            fs = new FileStream(filename,FileMode.Append,FileAccess.Write);            sw = new StreamWriter(fs);        }        public void Logger(string info)        {            sw.WriteLine(info);            sw.Flush();        }        public void Close()        {            sw.Close();            fs.Close();        }    }    public class RecordBoilerInfo    {        static void Logger(string info)        {            Console.WriteLine(info);        }        static void Main()        {            BoilerInfoLogger filelog = new BoilerInfoLogger("e:\\boiler.txt");            DelegateBoilerEvent boilerEvent = new DelegateBoilerEvent();            boilerEvent.BoilerEventLog += new DelegateBoilerEvent.BoilerLogHandler(Logger);            boilerEvent.BoilerEventLog += new DelegateBoilerEvent.BoilerLogHandler(filelog.Logger);            boilerEvent.LogProcess();            Console.ReadLine();            filelog.Close();            Console.ReadKey();        }    }}


原创粉丝点击