Standard delegate and Event _ 1, framework_1 in. Net Framework
The standard delegate in. Net Framework has been defined in the namespace System,
namespace System{ public delegate void EventHandler(object sender, EventArgs e);}
All events in the. Net Framwork class library are based on the EventHandler delegate.
The EventArgs parameter is customizable and must inherit the EventArgs class:
public class CustomEventArgs:EventArgs
There are three ways to publish events:
1. Use. net framework standard Delegation
public event EventHandler RaiseCustomEvent;
2. Custom EventArgs Parameters
public event CustomEventHandler RaiseCustomEvent;
3. Custom EventArgs parameter, generic expression
public event EventHandler<CustomEventArgs> RaiseCustomEvent;
How to: publish events that comply with the. net framework guidelines
There are two classes:
1. The Publisher class (Publisher) is the class that Sends (triggering Raise) events. This class does two things.
- Release events
- Compile the event trigger Program
Events (such as Load and Click) are released in the Form and control classes. The event trigger program can also be found as follows:
protected virtual void OnClick(EventArgs e);
2. Subscriber class, which receives the Receive (processing Handle) event class. This class does two things.
- Register an event
- Compile an event handler
When you double-click a button in Form1
private void button1_Click(object sender, EventArgs e) { }
It is actually writing the event processing program. In Form1.Designer. cs, the event will be automatically registered and double-clicked.
this.button1.Click += new System.EventHandler(this.button1_Click);
The following example shows how to add custom events in a standard way:
namespace DotNetEvents{ class Program { static void Main(string[] args) { Publisher pub = new Publisher(); Subscriber sub1 = new Subscriber("sub1", pub); Subscriber sub2 = new Subscriber("sub2", pub); //Call the method that raises the event pub.DoSomething(); Console.WriteLine("Press Enter to close this window."); Console.ReadKey(); } } //Define a class to hold custom event info public class CustomEventArgs:EventArgs { private string message; public CustomEventArgs (string s) { message = s; } 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; 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 oveeride 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 raise if(RaiseCustomEvent !=null ) { e.Message = String.Format(" at {0}", DateTime.Now.ToString()); RaiseCustomEvent(this, e); } } } //Class that subscribes to an event class Subscriber { private string id; public Subscriber (string ID,Publisher pub) { id = ID; //Subscribe to the event suing C# 2.0 syntax pub.RaiseCustomEvent += HandleCustomEvent; } //Define what action to take when the event is raised void HandleCustomEvent(object sender,CustomEventArgs e) { Console.WriteLine(id + " receive this message: {0}", e.Message); } }}