Event (Event) in C #

Events (Events) are the actions of users, such as pressing keys, clicking, moving mouse ... Applications need to respond to these events when they appear. For example, interrupts. Events (Events) are used to communicate within the process.

Events (Events) are the actions of users, such as pressing keys, clicking, moving mouse . Applications need to respond to these events when they appear. For example, interrupts. Events (Events) are used to communicate within the process.

Use Delegate with Event in C #

Events are declared and created in a class and linked to Event Handler by using Delegates within the same class or some other class. The class that contains the Event is used to publish the event. This is called the Publisher class. Some other classes that accept this Event are called Subscriber class. Events in C # use the Publisher-Subscriber model .

A Publisher in C # is an object that contains the definition of that event and delegate. Event-delegate relationships are also defined in this object. A Publisher class object summons the Event and it is notified to other objects.

A Subscriber in C # is an object that accepts the event and provides an Event Handler. Delegate in the Event Handler (Event Handler) class of the Subscriber class.

Declaring Event in C #

To declare an Event inside a class, first a delegate type for that Event must be declared. For example:

 public delegate void BoilerLogHandler ( string status ); 

Next, the Event itself is declared, using the event keyword in C #:

 //định nghĩa event dựa vào delegate ở trên public event BoilerLogHandler BoilerEventLog ; 

The above code defines a delegate with the name BoilerLogHandler and an Event named BoilerEventLog , which summons that delegate when it is created.

 using System ; namespace SampleApp { public delegate string MyDel ( string str ); class EventProgram { event MyDel MyEvent ; public EventProgram () { this . MyEvent += new MyDel ( this . WelcomeUser ); } public string WelcomeUser ( string username ) { return "TipsMake.com xin chào " + username ; } static void Main ( string [] args ) { EventProgram obj1 = new EventProgram (); string result = obj1 . MyEvent ( "bạn" ); Console . WriteLine ( result ); } } } 

Compiling and running the above C # program will produce the following results:

 Welcome to TipsMake.com 

Example 2

This example provides a simple application to troubleshoot a pot system over hot water. When the boiler maintenance engineer, boiler temperature and pressure are automatically recorded in a log file along with the notes of this maintenance engineer.

The program will be as follows:

 using System ;
using System . IO ; namespace QTMCsharp { 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 ); //định nghĩa sự kiện dựa vào delegate trên public event BoilerLogHandler BoilerEventLog ; public void LogProcess () { string remarks = "OK!" ; Boiler b = new Boiler ( 100 , 12 ); int t = b . getTemp (); int p = b . getPressure (); if ( t > 150 || t < 80 || p < 12 || p > 15 ) { remarks = "Cần duy trì" ; } OnBoilerEventLog ( "Thông tin bản ghi:n" ); OnBoilerEventLog ( "Nhiệt độ: " + t + "nÁp suất: " + p ); OnBoilerEventLog ( "nThông báo: " + 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 (); } } class TestCsharp { static void Logger ( string info ) { Console . WriteLine ( info ); } static void Main ( string [] args ) { Console . WriteLine ( "Ví dụ minh họa sự kiện trong C#" ); Console . WriteLine ( "-------------------------------" ); 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 (); Console . ReadKey (); filelog . Close (); } } }

And when you run this program you will get results:

 Example illustrating events in C # 
-------------------------------
Record information:

Temperature: 100
Pressure: 12

Notice: OK!

According to Tutorialspoint

Previous article: Delegate in C #

Next article: Collection in C #

5 ★ | 1 Vote