HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Event (Event) basically says is a user action, such as buttons, click, mouse movement, etc., or some emerging, such as system-generated notifications.Application needs to respond to an event when the event occurs. For example, the interrupt. Events are used for inter-process communication.
And the event is generated in the class declaration, and by using the same delegate class associated with the event handler or other classes. Class contains event for publishing events. This is calledthe publisher (publisher) class.Other accept the event type is calledfeed reader (subscriber) class.Events using thepublish - subscribe (publisher-subscriber) model.
Publisher (publisher) and contains the event is a delegate defined objects.Contact between events and delegates are also defined in this object. Publisher (publisher) class object calls this event, and notifies other objects.
Feed device (subscriber) is an accepted event and provide an event handler object.In Publisher (publisher) class delegate invocation feed reader (subscriber) class method (the event handler).
In the event declared inside the class, you must first declare a delegate type for the event. E.g:
public delegate void BoilerLogHandler (string status);
Then, declare the event itself, using theevent keyword:
// Based on the above definition of the event delegate public event BoilerLogHandler BoilerEventLog;
The above code defines a delegate and an event namedBoilerEventLognamedBoilerLogHandler,the event will be generated when the delegate is invoked.
using System; namespace SimpleEvent { using System; public class EventTest { private int value; public delegate void NumManipulationHandler (); public event NumManipulationHandler ChangeNum; protected virtual void OnNumChanged () { if (ChangeNum! = null) { ChangeNum (); } else { Console.WriteLine ( "Event fired!"); } } public EventTest (int n) { SetValue (n); } public void SetValue (int n) { if (value! = n) { value = n; OnNumChanged (); } } } public class MainClass { public static void Main () { EventTest e = new EventTest (5); e.SetValue (7); e.SetValue (11); Console.ReadKey (); } } }
When the above code is compiled and executed, it produces the following results:
Event Fired! Event Fired! Event Fired!
This example provides a simple hot water boiler system for troubleshooting applications. When a maintenance engineer to check the boiler, the boiler temperature and pressure will be as maintenance engineer notes automatically recorded to a log file.
using System; using System.IO; namespace BoilerEventAppl { // Boiler class 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; } } // Event Publisher class DelegateBoilerEvent { public delegate void BoilerLogHandler (string status); // Based on the above definition of the event delegate public event BoilerLogHandler BoilerEventLog; public void LogProcess () { string remarks = "O. K"; Boiler b = new Boiler (100, 12); int t = b.getTemp (); int p = b.getPressure (); if (t> 150 || t <80 || p <12 || p> 15) { remarks = "Need Maintenance"; } OnBoilerEventLog ( "Logging Info: \ n"); OnBoilerEventLog ( "Temparature" + t + "\ nPressure:" + p); OnBoilerEventLog ( "\ nMessage:" + remarks); } protected void OnBoilerEventLog (string message) { if (BoilerEventLog! = null) { BoilerEventLog (message); } } } Terms such reservations // written to the log file 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 (); } } // Event subscription is public class RecordBoilerInfo { static void Logger (string info) { Console.WriteLine (info); } // End of Logger static void Main (string [] args) { 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 (); filelog.Close (); } // End of main } // End of RecordBoilerInfo }
When the above code is compiled and executed, it produces the following results:
Logging info: Temperature 100 Pressure 12 Message: O. K