Event and wonder if you have that understanding too

Source: Internet
Author: User

Directory

    • The characteristics of the event.
    • Message instances and how the compiler treats events
    • Summarize

First, the incident

    • Public other classes can be registered and unregistered according to the rules.
    • Event I think it is an identifier that the compiler can identify to generate related event code.
    • Eventhander generic Delegate
      public delegate void Eventhandler<teventargs> (object sender, Teventargs e);

An event is a member of a class that presents a class with the ability to notify other objects of what has happened.

An event is a special kind of delegate. Other objects that conform to the rules of the method can register and unregister the event for concern.  

Second, the Mail instance

Requirements: When a new message is received, the notification is forwarded to the fax machine and other machines so that they can process the new message in their own way

1, Mail information class: Bearer mail related information, inherit EventArgs

     Public classNewmailinfo:eventargs {Private ReadOnly stringMsg from, to;  Public stringMSG {Get{returnmsg;} }         Public stringfrom {Get{return  from; } }         Public stringto {Get{returnto ;} }         PublicNewmailinfo (string_from,string_to,string_msg) {msg=_msg; to=_to;  from=_from; }    }

2, Mail management: Responsible for receiving new mail, and notify the registration of other classes (such as: fax machines, printers, etc.)
First, define a method to process the new message, assign the information to the message hosting class, and invoke the notification method.

         Public void Simulatenewmail (stringfromstring  msg)        {            new Newmailinfo (from, to, msg);            Onnewmail (e);        }

Second, the new mail notification feature notifies other registered classes.

                          Event  Eventhandler<newmailinfo> NewMail;  protected  virtual  void   Onnewmail (Newmailinfo e) {EventHandler  <newmailinfo > temp = System.Threading.Interlocked.CompareExchange (ref  NewMail, null , null  );  if  (temp! = null   this  , E); }

When the event keyword is detected by the compiler, it will:

Event EventHandler Msgevent;

translated into three parts :

1.

Null

    • A delegate reference chain that stores the method reference for the final registration.
    • Private delegate, private to prevent modification of the outer class.
    • The Add () and remove () methods are later used to add and remove references to this delegate reference chain.

2.

 Publicvoid Add_newmail (eventhandler<newmailinfo> value) { // current delegate chain eventhandler<newmailinfo> NewMail = this. _newmail; Eventhandler<newmailinfo> Prevhandler; do {prevhandler = newmail; Eventhandler<newmailinfo> Newhandler = (eventhandler<newmailinfo>) delegate.combine (PrevHandler, Value); NewMail = System.Threading.Interlocked.CompareExchange (ref this. _newmail, Newhandler, Prevhandler);} while (newmail! = prevhandler);}              

  • Compiler-generated add_newmail (eventhandler<newmailinfo> value) method
  • Backs up a copy of the current delegation chain. Eventhandler<newmailinfo> NewMail = this. _newmail;

  • Circulating body,eventhandler<newmailinfo> Newhandler = (eventhandler<newmailinfo>) Delegate.combine ( Prevhandler, value), append the method reference currently required for registration to the delegate chain Prevhandler, and generate a new delegate chain Newhandler
  • NewMail = System.Threading.Interlocked.CompareExchange (ref this. _newmail, Newhandler, Prevhandler); current this. _newmail is the same as the Prevhandler reference instance. If it is the same, assign the value of Newhandler to this . _newmail.

3 . The Remove () method, similar to the Add function.

The entire mail management class can be implemented as such.

     Public classMailmanager { Public EventEventhandler<newmailinfo>NewMail; protected Virtual voidOnnewmail (Newmailinfo e) {EventHandler<NewMailInfo> temp = System.Threading.Interlocked.CompareExchange (refNewMail,NULL,NULL); if(Temp! =NULL) Temp ( This, E); }         Public voidSimulatenewmail (string  from,stringTo,stringmsg) {Newmailinfo e=NewNewmailinfo ( from, to, MSG);        Onnewmail (e); }            }
View Code


3. How to register other classes

    Public class Fax    {        public  Fax (mailmanager mm)        {            mm. NewMail+ =faxmsg        ;        }  Public void Faxmsg (object  sender, Newmailinfo e)        {                        Console.Write (e.msg+"") From the fax machine "");}    }

The current use of + = to register, in fact, is also using the Add () method to register method reference.

Iii. Summary

    • The compiler encounters an event this flag will be translated. A private delegate that outward-open registers add and unregisters the Remove method. Events can only be modified by the current class, and only the registration and logoff methods are available externally.
    • The class that owns the event invokes the new method list maintained by the delegate to invoke the callback on the method, which, of course, has the ability to notify other objects.
    • External classes can be notified by registering events, and are handled in their own way.

Event and wonder if you have that understanding too

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.