In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to understand the .net incident, which may not be well understood by many people. in order to make you understand better, the editor summed up the following content for you. I hope you can get something from this article.
.net event
Overview of events
When something of interest to other classes or objects occurs, the class or object can notify them through events. The class that sends (or raises) the event is called the publisher, and the class that receives (or handles) the event is called the subscriber.
Characteristics
The publisher determines when the event is raised, and the subscriber determines what action to take in response to the event.
An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers.
Events without subscribers are never called.
Events are usually used to notify users of actions
If an event has multiple subscribers, multiple event handlers are called synchronously when the event is raised, or you can set an asynchronous invocation event.
You can use events to synchronize threads.
Events are based on the EventHandler delegate and the EventArgs base class.
Subscription and cancellation of events
If you want to write custom code that is called when an event is raised, you can subscribe to events published by other classes. For example, you can subscribe to the Click event of a button to cause the application to perform some useful actions when the user clicks the button.
Subscribe to event
If the Properties window is not visible, in Design view, right-click the form or control for which you want to create the event handler, and then select Properties.
At the top of the Properties window, click the events icon.
Double-click the event you want to create, and Visual C# creates an empty event handler method and adds it to your code. Alternatively, you can add code manually in Code view.
VS IDE subscription event
Programmatically subscribe to events
Defines an event handler method whose signature matches the delegate signature of the event. For example, if the event is based on the EventHandler delegate type, the following code represents the method stub
Use the addition assignment operator (+ =) to attach event handlers to events. In the following example, assume that an object named publisher has an event named RaiseCustomEvent. Note that the subscriber class needs to refer to the publisher class to subscribe to its events.
Oid HandleCustomEvent (object sender, CustomEventArgs a) {}
Publisher.RaiseCustomEvent + = HandleCustomEvent; publisher.RaiseCustomEvent + = new CustomEventHandler (HandleCustomEvent)
Anonymous methods subscribe to events
Use the addition assignment operator (+ =) to attach anonymous methods to the event. In the following example, assume that an object named publisher has an event named RaiseCustomEvent, and a CustomEventArgs class is defined to host some types of dedicated event information. Note that the subscriber class needs to refer to publisher to subscribe to its events.
Publisher.RaiseCustomEvent + = delegate (object o, CustomEventArgs e)
{
String s = o.ToString () + "+ e.ToString ()
Console.WriteLine (s)
}
Unsubscribe
To prevent the event handler from being called when an event is raised, you simply unsubscribe to the event. To prevent resource leakage, it is important to unsubscribe to the event before releasing the subscriber object. Before unsubscribing to an event, the multicast delegate that serves as the basis for the event in the publication object references the delegate that encapsulates the subscriber's event handler. As long as the published object contains the reference, no garbage collection is performed on the subscriber object.
Use the subtraction assignment operator (- =) to unsubscribe to the event. After all subscribers unsubscribe to an event, the event instance in the publisher class is set to null.
Publisher.RaiseCustomEvent-= HandleCustomEvent
Publish standard events
The following procedure demonstrates how to add events that conform to the standard .NET Framework pattern to your own classes and structures. All events in the .NET Framework class library are based on EventHandler delegates and are defined as follows.
Public delegate void EventHandler (object sender, EventArgs e)
Publish events in EventHandler mode
If there is no custom EventArgs class, the event type is a non-generic EventHandler delegate. It does not need to be declared because it has been declared in the System namespace contained by default in the C# project
If you are using a non-generic version of EventHandler and you have a custom class derived from EventArgs, declare your event in the publishing class and use your delegate as the type
If you are using a generic version, you do not need to customize the delegate. Instead, specify the event type as EventHandler and place the name of your own class in angle brackets.
(if you do not need to send custom data with events, skip this step and proceed directly to step 3. Declare the class in a scope visible to both the publisher class and the subscriber class, and add the members needed to retain the custom event data In this example, a simple string is returned.
If you are using a generic version of EventHandler, skip this step. Declare a delegate in the publishing class Give it a name that ends in EventHandler. The second parameter specifies the custom EventArgs type.
Use any of the following steps to declare events in the publishing class.
Public event EventHandler RaiseCustomEvent
Class Publisher
{
Public event CustomEventHandler RaiseCustomEvent
}
Public event EventHandler RaiseCustomEvent
Public delegate void CustomEventHandler (object sender, CustomEventArgs a)
Public class CustomEventArgs: EventArgs
{
Public CustomEventArgs (string s)
{
Msg = s
}
Private string msg
Public string Message
{
Get {return msg;}
}
}
Raise a base class event in a derived class
The following simple example demonstrates a standard way to declare events that can be raised from a derived class in a base class. This pattern is widely used in Windows forms classes in the .NET Framework base class library.
When creating a class that can be used as a base class for other classes, you must consider the fact that events are special types of delegates that can only be called from the class that declares them. A derived class cannot directly call an event declared in the base class. Although sometimes you may want an event to be raised only through the base class, in most cases you should allow the derived class to call the base class event. To do this, you can create a protected calling method in the base class that contains the event. By calling or overriding this calling method, the derived class can call the event indirectly.
Namespace BaseClassEvents {using System; using System.Collections.Generic; public class ShapeEventArgs: EventArgs {private double newArea; public ShapeEventArgs (double d) {newArea = d;} public double NewArea {get {return newArea;} public abstract class Shape {protected double area Public double Area {get {return area;} set {area = value;}} public event EventHandler ShapeChanged; public abstract void Draw (); protected virtual void OnShapeChanged (ShapeEventArgs e) {EventHandler handler = ShapeChanged; if (handler! = null) {handler (this, e) } public class Circle: Shape {private double radius; public Circle (double d) {radius = d; area = 3.14 * radius;} public void Update (double d) {radius = d; area = 3.14 * radius OnShapeChanged (new ShapeEventArgs (area));} protected override void OnShapeChanged (ShapeEventArgs e) {base.OnShapeChanged (e);} public override void Draw () {Console.WriteLine ("Drawing a circle");}} public class Rectangle: Shape {private double length; private double width Public Rectangle (double length, double width) {this.length = length; this.width = width; area = length * width;} public void Update (double length, double width) {this.length = length; this.width = width; area = length * width; OnShapeChanged (new ShapeEventArgs (area)) } protected override void OnShapeChanged (ShapeEventArgs e) {base.OnShapeChanged (e);} public override void Draw () {Console.WriteLine ("Drawing a rectangle");}} public class ShapeContainer {List _ list; public ShapeContainer () {_ list = new List () } public void AddShape (Shape s) {_ list.Add (s); s.ShapeChanged + = HandleShapeChanged;} private void HandleShapeChanged (object sender, ShapeEventArgs e) {Shape s = (Shape) sender; Console.WriteLine ("Received event. Shape area is now {0} ", e.NewArea); s.Draw ();}} class Test {static void Main (string [] args) {Circle C1 = new Circle (54); Rectangle R1 = new Rectangle (12,9); ShapeContainer sc = new ShapeContainer (); sc.AddShape (C1) Sc.AddShape (R1); c1.Update (57); r1.Update (7,7); Console.WriteLine (); Console.WriteLine ("Press Enter to exit"); Console.ReadLine ();}
Implement interface events
Interface can declare events. The following example shows how to implement interface events in a class. The implementation rules for interface events are basically the same as those for any interface method or property.
Implement interface events in a class
Declare the event in the class, and then call the event in the appropriate place.
Public interface IDrawingObject
{
Event EventHandler ShapeChanged
}
Public class MyEventArgs: EventArgs {… }
Public class Shape: IDrawingObject
{
Event EventHandler ShapeChanged
Void ChangeShape ()
{
/ / Do something before the event...
OnShapeChanged (new MyEventsArgs (…))
/ / or do something after the event.
}
Protected virtual void OnShapeChanged (MyEventArgs e)
{
If (ShapeChanged! = null)
{
ShapeChanged (this, e)
}
}
}
The following example shows how to handle the unusual situation where your class inherits from more than two interfaces, each with an event of the same name. In this case, you need to provide an explicit interface implementation for at least one of the events. When you write an explicit interface implementation for events, you must write add and remove event accessors. These two event accessors are usually provided by the compiler, but in this case the compiler cannot.
You can provide your own accessor to specify whether the two events are represented by the same event in your class or by different events. For example, according to the interface specification, if events should be raised at different times, you can associate each event with a separate implementation in the class. In the following example, the subscriber casts the shape reference to IShape or IDrawingObject to determine which OnDraw event he will receive.
Namespace WrapTwoInterfaceEvents {using System; public interface IDrawingObject {event EventHandler OnDraw;} public interface IShape {event EventHandler OnDraw;} public class Shape: IDrawingObject, IShape {event EventHandler PreDrawEvent; event EventHandler PostDrawEvent; event EventHandler IDrawingObject.OnDraw {add {PreDrawEvent + = value;} remove {PreDrawEvent-= value }} event EventHandler IShape.OnDraw {add {PostDrawEvent + = value;} remove {PostDrawEvent-= value;}} public void Draw () {EventHandler handler = PreDrawEvent; if (handler! = null) {handler (this, new EventArgs ()) } Console.WriteLine ("Drawing a shape."); handler = PostDrawEvent; if (handler! = null) {handler (this, new EventArgs ());} public class Subscriber1 {public Subscriber1 (Shape shape) {IDrawingObject d = (IDrawingObject) shape D.OnDraw + = new EventHandler (d_OnDraw);} void d_OnDraw (object sender, EventArgs e) {Console.WriteLine ("Sub1 receives the IDrawingObject event.");}} public class Subscriber2 {public Subscriber2 (Shape shape) {IShape d = (IShape) shape; d.OnDraw + = new EventHandler (d_OnDraw) } void d_OnDraw (object sender, EventArgs e) {Console.WriteLine ("Sub2 receives the IShape event.");}} public class Program {static void Main (string [] args) {Shape shape = new Shape (); Subscriber1 sub = new Subscriber1 (shape); Subscriber2 sub2 = new Subscriber2 (shape) Shape.Draw (); Console.WriteLine ("Press Enter to close this window."); Console.ReadLine ();}
Use dictionaries to store event instances
One use of accessor-declarations is to expose a large number of events without assigning fields to each event, but to use dictionaries to store these event instances. This is useful only if you have a large number of events, but you don't expect most of them to be implemented.
Public delegate void EventHandler1 (int I)
Public delegate void EventHandler2 (string s)
Public class PropertyEventsSample
{
Private System.Collections.Generic.Dictionary eventTable
Public PropertyEventsSample ()
{
EventTable = new System.Collections.Generic.Dictionary ()
EventTable.Add ("Event1", null)
EventTable.Add ("Event2", null)
}
Public event EventHandler1 Event1
{
Add
{
EventTable ["Event1"] = (EventHandler1) eventTable ["Event1"] + value
}
Remove
{
EventTable ["Event1"] = (EventHandler1) eventTable ["Event1"]-value
}
}
Public event EventHandler2 Event2
{
Add
{
EventTable ["Event2"] = (EventHandler2) eventTable ["Event2"] + value
}
Remove
{
EventTable ["Event2"] = (EventHandler2) eventTable ["Event2"]-value
}
}
Internal void RaiseEvent1 (int I)
{
EventHandler1 handler1
If (null! = (handler1 = (EventHandler1) eventTable ["Event1"]))
{
Handler1 (I)
}
}
Internal void RaiseEvent2 (string s)
{
EventHandler2 handler2
If (null! = (handler2 = (EventHandler2) eventTable ["Event2"]))
{
Handler2 (s)
}
}
}
Public class TestClass
{
Public static void Delegate1Method (int I)
{
System.Console.WriteLine (I)
}
Public static void Delegate2Method (string s)
{
System.Console.WriteLine (s)
}
Static void Main ()
{
PropertyEventsSample p = new PropertyEventsSample ()
P.Event1 + = new EventHandler1 (TestClass.Delegate1Method)
P.Event1 + = new EventHandler1 (TestClass.Delegate1Method)
P.Event1-= new EventHandler1 (TestClass.Delegate1Method)
P.RaiseEvent1 (2)
P.Event2 + = new EventHandler2 (TestClass.Delegate2Method)
P.Event2 + = new EventHandler2 (TestClass.Delegate2Method)
P.Event2-= new EventHandler2 (TestClass.Delegate2Method)
P.RaiseEvent2 ("TestString")
}
}
The asynchronous mode of the event
There are several ways to expose asynchronous functionality to client code. The event-based asynchronous pattern specifies the recommended way for classes to display asynchronous behavior. For relatively simple multithreaded applications, the BackgroundWorker component provides a simple solution. For more complex asynchronous applications, consider implementing a class that conforms to the event-based asynchronous pattern.
Perform time-consuming tasks (such as downloads and database operations) "in the background" without interrupting your application.
Multiple operations are performed at the same time, and each operation is notified when it is completed.
Wait for the resource to become available, but will not stop ("suspend") your application.
Use the familiar event and delegate model to communicate with pending asynchronous operations.
After reading the above, do you have any further understanding of how to understand the .net event? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.