事件
一.事件概述
事件具有以下特點:
-
發(fā)行者確定何時引發(fā)事件,訂戶確定執(zhí)行何種操作來響應該事件。
-
一個事件可以有多個訂戶。一個訂戶可處理來自多個發(fā)行者的多個事件。
-
沒有訂戶的事件永遠不會被調(diào)用。
-
事件通常用于通知用戶操作(如:圖形用戶界面中的按鈕單擊或菜單選擇操作)。
-
如果一個事件有多個訂戶,當引發(fā)該事件時,會同步調(diào)用多個事件處理程序。要異步調(diào)用事件,請參見使用異步方式調(diào)用同步方法。
-
可以利用事件同步線程。
-
在 .NET Framework 類庫中,事件是基于 EventHandler 委托和 EventArgs 基類的。
二. 使用.
1.定義委托. (也可以基于系統(tǒng)的委托,這樣就可以不用定義啦.)
2.定義一個事件變量(基于定義的委托.) //event 委托類型 變量名稱.
3.發(fā)布
4.訂閱
代碼
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
// Define a class to hold custom event info
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(string s)
{
message = s;
}
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
}
// Class that publishes an event
class Publisher
{
// Declare the event using EventHandler<T>
public event EventHandler<CustomEventArgs> RaiseCustomEvent; //2.定義一個事件變量.
public void DoSomething()
{
// Write some code that does something useful here
// then raise the event. You can also raise an event
// before you execute a block of code.
OnRaiseCustomEvent(new CustomEventArgs("Did something"));
}
// Wrap event invocations inside a protected virtual method
// to allow derived classes to override the event invocation behavior
protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
EventHandler<CustomEventArgs> handler = RaiseCustomEvent;
// Event will be null if there are no subscribers
if (handler != null)
{
// Format the string to send inside the CustomEventArgs parameter
e.Message += String.Format(" at {0}", DateTime.Now.ToString());
// Use the () operator to raise the event.
handler(this, e); //3.發(fā)布
}
}
}
//Class that subscribes to an event
class Subscriber
{
private string id;
public Subscriber(string ID)
{
id = ID;
// Subscribe to the event using C# 2.0 syntax
//pub.RaiseCustomEvent += HandleCustomEvent;
}
// Define what actions to take when the event is raised.
public void HandleCustomEvent(object sender, CustomEventArgs e)
{
Console.WriteLine(id + " received this message: {0}", e.Message);
}
}
class Program
{
static void Main(string[] args)
{
Publisher pub = new Publisher();
Subscriber sub1 = new Subscriber("sub1");
Subscriber sub2 = new Subscriber("sub2");
pub.RaiseCustomEvent += sub1.HandleCustomEvent; //4.訂閱
pub.RaiseCustomEvent += sub2.HandleCustomEvent;
// Call the method that raises the event.
pub.DoSomething();
// Keep the console window open
Console.WriteLine("Press Enter to close this window.");
Console.ReadLine();
}
}
}
{
using System;
using System.Collections.Generic;
// Define a class to hold custom event info
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(string s)
{
message = s;
}
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
}
// Class that publishes an event
class Publisher
{
// Declare the event using EventHandler<T>
public event EventHandler<CustomEventArgs> RaiseCustomEvent; //2.定義一個事件變量.
public void DoSomething()
{
// Write some code that does something useful here
// then raise the event. You can also raise an event
// before you execute a block of code.
OnRaiseCustomEvent(new CustomEventArgs("Did something"));
}
// Wrap event invocations inside a protected virtual method
// to allow derived classes to override the event invocation behavior
protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
EventHandler<CustomEventArgs> handler = RaiseCustomEvent;
// Event will be null if there are no subscribers
if (handler != null)
{
// Format the string to send inside the CustomEventArgs parameter
e.Message += String.Format(" at {0}", DateTime.Now.ToString());
// Use the () operator to raise the event.
handler(this, e); //3.發(fā)布
}
}
}
//Class that subscribes to an event
class Subscriber
{
private string id;
public Subscriber(string ID)
{
id = ID;
// Subscribe to the event using C# 2.0 syntax
//pub.RaiseCustomEvent += HandleCustomEvent;
}
// Define what actions to take when the event is raised.
public void HandleCustomEvent(object sender, CustomEventArgs e)
{
Console.WriteLine(id + " received this message: {0}", e.Message);
}
}
class Program
{
static void Main(string[] args)
{
Publisher pub = new Publisher();
Subscriber sub1 = new Subscriber("sub1");
Subscriber sub2 = new Subscriber("sub2");
pub.RaiseCustomEvent += sub1.HandleCustomEvent; //4.訂閱
pub.RaiseCustomEvent += sub2.HandleCustomEvent;
// Call the method that raises the event.
pub.DoSomething();
// Keep the console window open
Console.WriteLine("Press Enter to close this window.");
Console.ReadLine();
}
}
}


浙公網(wǎng)安備 33010602011771號