Event (publisher) sender and event receiver (subscriber)
First Step: Sender class defines event members
Step Two: Define the method that triggers the event in the receiver class
Event receiver (class notified when an event occurs)
Step three: When the sender fires an event, all handlers for the event's method list are called.
Attention:
Event Handlers : The method that is registered by the Subscriber to the event, which occurs when the publisher triggers the event. Typically event handler methods can be defined in the class in which the event resides, or in different classes.
5 Source code components when using an event
Component Description:
delegate type declaration
Event and event handlers must have a common signature and return type, which is described by a delegate type
event handler Declaration
The method declaration in the Subscriber class that is executed in event-triggered execution.
Event Declaration
declare the event to be Public
Event Registration
subscribers must subscribe to events to be notified when it triggers .
code that triggers the event
in the Publisher class " triggering " event and causes the code to invoke all event handlers that are registered.
declaring events
The Publisher class must provide an event object.
Notice the following points in the event statement
event declarations in a class
The name of the delegate type is required, and any handler attached to the event (such as registration) must match the signature and return type of the delegate type.
The event must be declared public so that other classes can attach (register) event handlers on it.
You cannot use an object creation expression (new expression) to create its object.
An event is a member of a class, and an event declaration requires the name of a delegate type, and a delegate type can be declared.
such as: Public delegate void MyDelegate (); declaring a delegate
Class Send
{
Public event MyDelegate Eventmydele; declaring delegate type events
Public void Oneventmydele ()// method of triggering the event's code
{
if (eventmydele!=null)// before the triggering event and NULL compare to see if a registered event handler is included, if the event is NULL, The program is not registered and cannot be executed. (Confirm that there is a way to do it)
Eventmydele ();// Triggering Events Same as calling methods.
} }
Class ACCEPT1
{
Public Accept1 (Send s)
{S.eventmydele + = Newmydelegate (S_eventmydele);}
// Use += operator adds an event handler for the event. The event handler is located to the right of the operator.
public void S_eventmydele ()
{
Console.WriteLine ("Hello");
}
}
Class Accept2
{
Public Accept2 (send send)
{
Send.eventmydele + = Newmydelegate (Send_eventmydele);
// Use += operator adds an event handler for the event. The event handler is located to the right of the operator.
}
void Send_eventmydele ()
{
Console.WriteLine ("World");
}
}
Class Program
{
static void Main (string[] args)
{
Send s = new Send ();
ACCEPT1 AC1 = new ACCEPT1 (s);
Accept2 AC2 = new Accept2 (s);
S.oneventmydele ();
Console.readkey (TRUE);
}
}
}
Usage of standard events
Windows applications are event-driven and can be interrupted by events at any time when the program is running. For the use of events,. NET Framework provides a standard pattern.
The standard pattern used by events is simply the Eventhandle delegate type of the System namespace declaration.
public delegate void EventHandler (object sender, EventArgs e);
Attention:
The second parameter of the EventHandler delegate type is the object of the EventArgs class, which is declared in the System namespace.
EventArgs designed to not pass any data. (is an empty class , no members)
Used for event handlers that do not need to pass data.
object, EventArgs is the base class, so EventHandler can provide a generic signature for all events and event handlers.
Switch the above program to a standard event
public delegate void MyDelegate ();
Class Send
{public event EventHandler Eventmydele;
public void Oneventmydele ()
{if (eventmydele!=null)
Eventmydele (this,null); There are two parameters
}
}
Class ACCEPT1
{Public Accept1 (Send s)
{S.eventmydele + = new EventHandler (S_eventmydele);}
Public void S_eventmydele (Object Sender,eventargs e)// to add Object Sender,eventargs e
{Console.WriteLine ("Hello")}
}
Class Accept2
{Public Accept2 (send send)
{Send.eventmydele + = new EventHandler (Send_eventmydele);
}
void Send_eventmydele (Object Sender,eventargs e)
{Console.WriteLine ("World");}
}
Class Program
{
static void Main (string[] args)
{Send s = new Send ();
ACCEPT1 AC1 = new ACCEPT1 (s);
Accept2 AC2 = new Accept2 (s);
S.oneventmydele ();
Console.readkey (TRUE);
} }
}
To pass data by extending EventArgs
In order to pass in data to the second parameter of your event handler and conform to standard practice, you need to declare a custom class derived from EventArgs that holds the data that we need to pass in. The above example
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace ConsoleApplication1
{
Class Sendeventargs:eventargs
{
public string Message
{
Get;private set;
}
Public Sendeventargs (String m)
{
This.message = m;
}
}
Class Send
{
Public event eventhandler<sendeventargs>eventmydele;
public void Oneventmydele ()//in event-triggered code to pass data to custom event argument classes
{
Sendeventargs sed = Newsendeventargs ("C # Study");
if (eventmydele!=null)
Eventmydele (this, SED); There are two parameters
}
}
Class ACCEPT1
{
Public Accept1 (Send s)
{S.eventmydele + = new eventhandler<sendeventargs> (S_eventmydele);}
public void S_eventmydele (object sender, Sendeventargse)//To add Objectsender, EventArgs E
{
Console.Write ("Hello");
}
}
Class Accept2
{
Public Accept2 (send send)
{
Send.eventmydele + = neweventhandler<sendeventargs> (Send_eventmydele);
}
void Send_eventmydele (object sender, Sendeventargse)
{
Console.WriteLine ("World" +e.message);
}
}
Class Program
{
static void Main (string[] args)
{
Send s = new Send ();
ACCEPT1 AC1 = new ACCEPT1 (s);
Accept2 AC2 = new Accept2 (s);
S.oneventmydele ();
Console.readkey (TRUE);
}
}
}
About C # Event Summary