中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

C# 事件(Event)

事件(Event) 基本上說(shuō)是一個(gè)用戶操作,如按鍵、點(diǎn)擊、鼠標(biāo)移動(dòng)等等,或者是一些提示信息,如系統(tǒng)生成的通知。應(yīng)用程序需要在事件發(fā)生時(shí)響應(yīng)事件。例如,中斷。

C# 中使用事件機(jī)制實(shí)現(xiàn)線程間的通信。

通過(guò)事件使用委托

事件在類中聲明且生成,且通過(guò)使用同一個(gè)類或其他類中的委托與事件處理程序關(guān)聯(lián)。包含事件的類用于發(fā)布事件。這被稱為 發(fā)布器(publisher) 類。其他接受該事件的類被稱為 訂閱器(subscriber) 類。事件使用 發(fā)布-訂閱(publisher-subscriber) 模型。

發(fā)布器(publisher) 是一個(gè)包含事件和委托定義的對(duì)象。事件和委托之間的聯(lián)系也定義在這個(gè)對(duì)象中。發(fā)布器(publisher)類的對(duì)象調(diào)用這個(gè)事件,并通知其他的對(duì)象。

訂閱器(subscriber) 是一個(gè)接受事件并提供事件處理程序的對(duì)象。在發(fā)布器(publisher)類中的委托調(diào)用訂閱器(subscriber)類中的方法(事件處理程序)。

聲明事件(Event)

在類的內(nèi)部聲明事件,首先必須聲明該事件的委托類型。例如:

public delegate void BoilerLogHandler(string status);

然后,聲明事件本身,使用 event 關(guān)鍵字:

// 基于上面的委托定義事件
public event BoilerLogHandler BoilerEventLog;

上面的代碼定義了一個(gè)名為 BoilerLogHandler 的委托和一個(gè)名為 BoilerEventLog 的事件,該事件在生成的時(shí)候會(huì)調(diào)用委托。

實(shí)例

實(shí)例 1

using System;
namespace SimpleEvent
{
? using System;
? /***********發(fā)布器類***********/
? public class EventTest
? {
? ? private int value;

? ? public delegate void NumManipulationHandler();


? ? public event NumManipulationHandler ChangeNum;
? ? protected virtual void OnNumChanged()
? ? {
? ? ? if ( ChangeNum != null )
? ? ? {
? ? ? ? ChangeNum(); /* 事件被觸發(fā) */
? ? ? }else {
? ? ? ? Console.WriteLine( "event not fire" );
? ? ? ? Console.ReadKey(); /* 回車?yán)^續(xù) */
? ? ? }
? ? }


? ? public EventTest()
? ? {
? ? ? int n = 5;
? ? ? SetValue( n );
? ? }


? ? public void SetValue( int n )
? ? {
? ? ? if ( value != n )
? ? ? {
? ? ? ? value = n;
? ? ? ? OnNumChanged();
? ? ? }
? ? }
? }


? /***********訂閱器類***********/

? public class subscribEvent
? {
? ? public void printf()
? ? {
? ? ? Console.WriteLine( "event fire" );
? ? ? Console.ReadKey(); /* 回車?yán)^續(xù) */
? ? }
? }

? /***********觸發(fā)***********/
? public class MainClass
? {
? ? public static void Main()
? ? {
? ? ? EventTest e = new EventTest(); /* 實(shí)例化對(duì)象,第一次沒(méi)有觸發(fā)事件 */
? ? ? subscribEvent v = new subscribEvent(); /* 實(shí)例化對(duì)象 */
? ? ? e.ChangeNum += new EventTest.NumManipulationHandler( v.printf ); /* 注冊(cè) */
? ? ? e.SetValue( 7 );
? ? ? e.SetValue( 11 );
? ? }
? }
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

event not fire
event fire
event fire

本實(shí)例提供一個(gè)簡(jiǎn)單的用于熱水鍋爐系統(tǒng)故障排除的應(yīng)用程序。當(dāng)維修工程師檢查鍋爐時(shí),鍋爐的溫度和壓力會(huì)隨著維修工程師的備注自動(dòng)記錄到日志文件中。

實(shí)例 2

using System;
using System.IO;

namespace BoilerEventAppl
{

? ?// boiler 類
? ?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;
? ? ? }
? ?}
? ?// 事件發(fā)布器
? ?class DelegateBoilerEvent
? ?{
? ? ? public delegate void BoilerLogHandler(string status);

? ? ? // 基于上面的委托定義事件
? ? ? public event BoilerLogHandler BoilerEventLog;

? ? ? public void LogProcess()
? ? ? {
? ? ? ? ?string remarks = "O. 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 = "Need Maintenance";
? ? ? ? ?}
? ? ? ? ?OnBoilerEventLog("Logging Info:n");
? ? ? ? ?OnBoilerEventLog("Temparature " + t + "nPressure: " + p);
? ? ? ? ?OnBoilerEventLog("nMessage: " + remarks);
? ? ? }

? ? ? protected void OnBoilerEventLog(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);
? ? ? }
? ? ? public void Close()
? ? ? {
? ? ? ? ?sw.Close();
? ? ? ? ?fs.Close();
? ? ? }
? ?}
? ?// 事件訂閱器
? ?public class RecordBoilerInfo
? ?{
? ? ? static void Logger(string info)
? ? ? {
? ? ? ? ?Console.WriteLine(info);
? ? ? }//end of Logger

? ? ? static void Main(string[] args)
? ? ? {
? ? ? ? ?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();
? ? ? }//end of main

? ?}//end of RecordBoilerInfo
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

Logging info:

Temperature 100
Pressure 12

Message: O. K