Author: juky_huang Simple explanation of the event:
An event is a message sent by an object to signal the occurrence of an operation. Operations may be caused by user interaction (such as mouse clicks) or may be triggered by some other program logic. The object that triggers (triggers) an event is called the event sender. The object that captures an event and responds to it is called the event recipient.
In event communication, the event sender class does not know which object or method will receive (process) the event it raises. What is needed is the existence of a medium (or pointer-like mechanism) between the source and the receiver. .NET Framework defines a special type (Delegate) that provides the function pointer function.
Unlike other classes, a delegate class has a signature, and it can only reference methods that match its signature. In this way, the delegate is equivalent to a type-safe function pointer or a callback.
Steps required to use events in C#:
Create a delegate
Associate the created delegate with a specific event (many events in the .Net class library have been customized, so they have a corresponding delegate. When writing the associated event handler - that is, when an event occurs, we need to have the same signature as the delegate when the method we want to execute when an event occurs)
Write event handlers
Generate a delegate instance using the written event handler
Add this delegate instance to the event list that generates the event object. This process is also called subscription events
The process of event generation and implementation in C#:
Define A as an instance that generates an event, a is an event generated by A
Define B as an instance of receiving an event and b as a method of processing an event
A because the user (program writer or program user) or the system generates an event (for example, clicking a Button, generating a Click event)
A notifies B of this event through the delegate object in the event list
B receives an event notification (actually using delegate to realize event reception)
Calling the method to complete the event processing
The following is an example of "Introduction to C#" and give some explanation:
//===============================
//Event definition, that is, A mentioned above
//============================================
using System;
using ;
namespace Ch12Ex02
{
/// <summary>
/// Summary description of Connection.
/// </summary>
///
public delegate void MessageHandler(string messageText);//Create a delegate---Step 1
public class Connection
{
public event MessageHandler MessageArrived;//Associate the created delegate with a specific event, where the specific event is MessageArrived --Step 2*/
/*What is worth noting about in the above statement is that after the MessageArrived method is associated with the MessageHandler, the message delivery will be implemented through the MessageHandler delegation. Therefore, if you want to receive this message, you must be able to support the MessageHandler delegation, that is, you must have a signature like the delegate.
private Timer pollTimer;
public Connection()
{
//
// TODO: Add constructor logic here
//
pollTimer=new Timer(100);
+=new ElapsedEventHandler(CheckForMessage);
}
public void Connect()
{
();
}
public void Disconnect()
{
();
}
public void CheckForMessage(object sender,ElapsedEventArgs e)
{
("Check for message.");
Random random=new Random();
if(((9)==0)&&(MessageArrived!=null))
{
MessageArrived("Hello Mum!");//The program writer generates a message himself, and the content of the message is Hello Mum!
}
}
}
}
//===============================
//The class that receives the event is B mentioned above
//=========================================
using System;
namespace Ch12Ex02
{
/// <summary>
/// Summary description of Display.
/// </summary>
public class Display
{
public Display()
{
//
// TODO: Add constructor logic here
//
}
public void DisplayMessage(string message) //The final processing function of the a event, that is, in the main function, we will use this function to implement a delegate instance and add it to the MessageArrived event list of A--Step 3
{
("Message Arrived:{0}",message);
}
}
}
//=====================================
//A console executable class mainly uses instances of the above two classes
//==============================================
using System;
namespace Ch12Ex02
{
/// <summary>
/// Class1 summary description.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point of the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code here to start the application
//
Connection myConnection=new Connection();
Display myDisplay=new Display();
+=new MessageHandler();//Add the delegate to the current A event list----Step 4 and 5
();
();
}
}
}
Noteworthy code:
public delegate void MessageHandler(string messageText);//Delegate definition
public event MessageHandler MessageArrived;//Define an event and associate it with a delegation
+=new MessageHandler();//Create a delegate instance and add it to the event list through the += operator. The += operator is very useful here.
An event is a message sent by an object to signal the occurrence of an operation. Operations may be caused by user interaction (such as mouse clicks) or may be triggered by some other program logic. The object that triggers (triggers) an event is called the event sender. The object that captures an event and responds to it is called the event recipient.
In event communication, the event sender class does not know which object or method will receive (process) the event it raises. What is needed is the existence of a medium (or pointer-like mechanism) between the source and the receiver. .NET Framework defines a special type (Delegate) that provides the function pointer function.
Unlike other classes, a delegate class has a signature, and it can only reference methods that match its signature. In this way, the delegate is equivalent to a type-safe function pointer or a callback.
Steps required to use events in C#:
Create a delegate
Associate the created delegate with a specific event (many events in the .Net class library have been customized, so they have a corresponding delegate. When writing the associated event handler - that is, when an event occurs, we need to have the same signature as the delegate when the method we want to execute when an event occurs)
Write event handlers
Generate a delegate instance using the written event handler
Add this delegate instance to the event list that generates the event object. This process is also called subscription events
The process of event generation and implementation in C#:
Define A as an instance that generates an event, a is an event generated by A
Define B as an instance of receiving an event and b as a method of processing an event
A because the user (program writer or program user) or the system generates an event (for example, clicking a Button, generating a Click event)
A notifies B of this event through the delegate object in the event list
B receives an event notification (actually using delegate to realize event reception)
Calling the method to complete the event processing
The following is an example of "Introduction to C#" and give some explanation:
//===============================
//Event definition, that is, A mentioned above
//============================================
using System;
using ;
namespace Ch12Ex02
{
/// <summary>
/// Summary description of Connection.
/// </summary>
///
public delegate void MessageHandler(string messageText);//Create a delegate---Step 1
public class Connection
{
public event MessageHandler MessageArrived;//Associate the created delegate with a specific event, where the specific event is MessageArrived --Step 2*/
/*What is worth noting about in the above statement is that after the MessageArrived method is associated with the MessageHandler, the message delivery will be implemented through the MessageHandler delegation. Therefore, if you want to receive this message, you must be able to support the MessageHandler delegation, that is, you must have a signature like the delegate.
private Timer pollTimer;
public Connection()
{
//
// TODO: Add constructor logic here
//
pollTimer=new Timer(100);
+=new ElapsedEventHandler(CheckForMessage);
}
public void Connect()
{
();
}
public void Disconnect()
{
();
}
public void CheckForMessage(object sender,ElapsedEventArgs e)
{
("Check for message.");
Random random=new Random();
if(((9)==0)&&(MessageArrived!=null))
{
MessageArrived("Hello Mum!");//The program writer generates a message himself, and the content of the message is Hello Mum!
}
}
}
}
//===============================
//The class that receives the event is B mentioned above
//=========================================
using System;
namespace Ch12Ex02
{
/// <summary>
/// Summary description of Display.
/// </summary>
public class Display
{
public Display()
{
//
// TODO: Add constructor logic here
//
}
public void DisplayMessage(string message) //The final processing function of the a event, that is, in the main function, we will use this function to implement a delegate instance and add it to the MessageArrived event list of A--Step 3
{
("Message Arrived:{0}",message);
}
}
}
//=====================================
//A console executable class mainly uses instances of the above two classes
//==============================================
using System;
namespace Ch12Ex02
{
/// <summary>
/// Class1 summary description.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point of the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code here to start the application
//
Connection myConnection=new Connection();
Display myDisplay=new Display();
+=new MessageHandler();//Add the delegate to the current A event list----Step 4 and 5
();
();
}
}
}
Noteworthy code:
public delegate void MessageHandler(string messageText);//Delegate definition
public event MessageHandler MessageArrived;//Define an event and associate it with a delegation
+=new MessageHandler();//Create a delegate instance and add it to the event list through the += operator. The += operator is very useful here.