C# 事件(Event)
阿新 • • 發佈:2020-12-30
轉載:https://www.runoob.com/csharp/csharp-event.html
事件(Event) 基本上說是一個使用者操作,如按鍵、點選、滑鼠移動等等,或者是一些提示資訊,如系統生成的通知。應用程式需要在事件發生時響應事件。例如,中斷。
C# 中使用事件機制實現執行緒間的通訊。
通過事件使用委託
事件在類中宣告且生成,且通過使用同一個類或其他類中的委託與事件處理程式關聯。包含事件的類用於釋出事件。這被稱為 釋出器(publisher) 類。其他接受該事件的類被稱為 訂閱器(subscriber) 類。事件使用 釋出-訂閱(publisher-subscriber) 模型。
釋出器(publisher)
訂閱器(subscriber) 是一個接受事件並提供事件處理程式的物件。在釋出器(publisher)類中的委託呼叫訂閱器(subscriber)類中的方法(事件處理程式)。
宣告事件(Event)
在類的內部宣告事件,首先必須宣告該事件的委託型別。例如:
publicdelegatevoidBoilerLogHandler(string status);
然後,宣告事件本身,使用 event 關鍵字:
// 基於上面的委託定義事件publiceventBoilerLogHandlerBoilerEventLog;
上面的程式碼定義了一個名為 BoilerLogHandler 的委託和一個名為 BoilerEventLog 的事件,該事件在生成的時候會呼叫委託。
例項
例項 1
using System;namespace SimpleEvent
{
using System;
/***********釋出器類***********/
public class EventTest
{
private int value;
public delegate void NumManipulationHandler();
public event NumManipulationHandler ChangeNum;
protected virtual void OnNumChanged()
{
if ( ChangeNum != null )
{
ChangeNum(); /* 事件被觸發 */
}else {
Console.WriteLine( "event not fire" );
Console.ReadKey(); /* 回車繼續 */
}
}
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(); /* 回車繼續 */
}
}
/***********觸發***********/
public class MainClass
{
public static void Main()
{
EventTest e = new EventTest(); /* 例項化物件,第一次沒有觸發事件 */
subscribEvent v = new subscribEvent(); /* 例項化物件 */
e.ChangeNum += new EventTest.NumManipulationHandler( v.printf ); /* 註冊 */
e.SetValue( 7 );
e.SetValue( 11 );
}
}
}
當上面的程式碼被編譯和執行時,它會產生下列結果:
eventnot fire event fire event fire
本例項提供一個簡單的用於熱水鍋爐系統故障排除的應用程式。當維修工程師檢查鍋爐時,鍋爐的溫度和壓力會隨著維修工程師的備註自動記錄到日誌檔案中。
例項 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;
}
}
// 事件釋出器
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
}
當上面的程式碼被編譯和執行時,它會產生下列結果:
Logging info:
Temperature 100
Pressure 12
Message: O. K