C # advanced programming event study notes

Source: Internet
Author: User

Reference C # advanced programming (version 6):

Windows-based programs are message-based. This indicates that the application communicates with the application through windows, and Windows uses predefined messages to communicate with the application. These messages are structures that contain various information. The information used by applications and Windows determines the next step. Before the development environment such as MFC or visual basic is released, developers must process messages sent to applications by windows. Visual Basic and today's. Net encapsulate these sent messages in events. To respond to a message, you must handle the corresponding event. A common example is that after you click a button in the form, Windows sends a wm_mouseclick message to the button Message Processing Program (sometimes called a Windows process or wndproc. For. NET developers, This is the button click event.

When developing an object-based application, another object communication method is required. When something interesting happens to an object, you need to notify other objects of any changes. Here we need to use events again. Just as. NET Framework encapsulates Windows messages in events, events can also be used as communication media between objects.

A delegate is used to encapsulate an event when an application receives a message.

  

Therefore, to understand events, you must first understand these two concepts, which are the basic part of event programming: event generator (event class) and event processor (event class ).

Event generator refers to the class, application, or component that triggers some events.

An event receiver is any application, object, or component that is notified when an event occurs. It is used to respond to and process events triggered by a generator.

The event generator does not know that the receiver will handle the event, and the receiver needs a method to handle the event somewhere. Execute this event handler when a registered event occurs. Therefore, you need to introduce the delegate to implement this function, that is, let the delegate act as the medium between the event generator and the event receiver.

To use an event, follow these steps:

1) Create an event class

2) create an event generator and a delegate

3) associate the created delegate with a specific event class

4) Write event receivers and event handlers

5) use the compiled event handler to generate a delegated instance

6) Add the delegated instance to the event list that generates the event object. This process is called event subscription.

(Reference: http://www.cnblogs.com/michaelxu/archive/2008/04/02/1134217.html)

The Code is as follows:

1. Create an event class. Note that eventargs is the base class that contains the event class. This class does not contain event data, when an event is triggered, you can use this class without passing event status information to the event handler. If the event class needs to use status information, the application needs to derive a class from this class to save the status information. The event class has two types of information: eventcode and eventvalue. Therefore, the actioneventargs class must be derived.

  1 public class ActionEventArgs:EventArgs
2 {
3 private string eventCode;
4 private string eventValue;
5
6 public ActionEventArgs(string _eventCode,string _eventValue)
7 {
8 this.eventCode=_eventCode;
9 this.eventValue=_eventValue;
10 }
11
12 public string EventCode
13 {
14 get
15 {
16 return this.eventCode;
17 }
18 set
19 {
20 this.eventCode=value;
21 }
22 }
23
24 public string EventVaue
25 {
26 get
27 {
28 return this.eventValue;
29 }
30 set
31 {
32 this.eventValue=value;
33 }
34 }
35 }

 

2. Create an event generator and a delegate, and associate the delegate with a specific event.

 1 public class eventactor
2 {
3 // create a delegate
4 Public Delegate void actioneventhandle (Object sender, actioneventargs E );
5 // associate the created delegate with a specific event
6 public event actioneventhandle actor;
7
8 Public void excute ()
9 {
10 string code = console. Readline ();
11 string value = console. Readline ();
12 actioneventargs = new actioneventargs (code, value );
13 actor (this, actioneventargs );
14}
15}

The above actor (this, actioneventargs) is used to trigger an event and submit the event to the actor delegate for processing. The delegate can specify a program to process the event, that is, the event receiver processes the event, this indicates that the object that triggers the event is itself, and actioneventargs contains the event status information.

3. Write the event receiver and event processing program, generate a delegate instance and subscribe to the event.

This class generates a delegate instance and adds the delegate instance to the event list of the event generator. This is the event subscription.

  1 public class EventReceiver
2 {
3 public EventReceiver(EventActor actEvent)
4 {
5 actEvent.Actor+=new EventActor.ActionEventHandle(this.EventResponse);
6 }
7
8 private void EventResponse(object sender,ActionEventArgs e)
9 {
10 Console.WriteLine("EventCode:{0},EventValue:{1}",e.EventCode,e.EventValue);
11 }
12 }

 

4. program entry

 1 public class Test
2 {
3 public static void Main()
4 {
5 EventActor actor=new EventActor();
6 EventReceiver receiver=new EventReceiver(actor);
7 actor.Excute();
8 }
9 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.