[Go] Custom events

Source: Internet
Author: User

Debuglzq the original. NET Custom Event Summary

Everyone should be familiar with the incident and believe that every programmer can say "event?" I know ah ~ ", how can I customize events?" Maybe someone's heart is not so full of clout.

In many places have been cited to the flooding of an example to write, the initiator did not go into the drill. Description is like this: When the thief enters, the event sender (dog) sends a dog called event (Custom), (event recipient) The host subscribes to the dog called the event and handles (catches the thief), the parameters of the event pass the number of thieves. The following is a concrete step in the implementation---has been identified in the code comments for the blogger to abstract the process of defining the event.

Using system;using system.collections.generic;using system.linq;using system.text;namespace custom Events {//http:// Www.cnblogs.com/DebugLZQ//Custom Event Step Sample code//0. Defines the parameters for event passing class Alarmeventargs:eventargs {public int nu        Mberofthief;        public AlarmEventArgs (int n) {numberofthief = n; }}//The issuer of the event class Dog {//1. Delegate for declaring an event public delegate void AlarmEventHandler (Object sender, Alarm        EventArgs e);        2. Declaring events public event AlarmEventHandler Alarm; 3. The function that raised the event public void Onalarm (AlarmEventArgs e) {if (this.                Alarm! = null) {Console.WriteLine ("The Dog is Wangwang ..."); This.                            Alarm (this, e); }}}//Event Subscriber class Host {//4. Subscribe to Event public Host (dog dog) {dog.        Alarm + = new Dog.alarmeventhandler (hosthandleevent); }//5. event handler public void Hosthandleevent (OBject sender, AlarmEventArgs e) {Console.WriteLine ("Host caught the thief!        The sum is "+e.numberofthief"; }} class Program {static void Main (string[] args) {Console.WriteLine (            "Program is running ...");            Dog dog = new Dog ();            Host host = new host (dog);            Console.WriteLine ("Someone is coming ...");            6. Trigger Event AlarmEventArgs AlarmEventArgs = new AlarmEventArgs (3) at the appropriate time; Dog.            Onalarm (AlarmEventArgs);        Console.readkey (); }    }}

The results of the program run as follows:

The example is very simple, I write in order, in the code also has detailed step comment, not much to say. General experience, custom things are often very useful, and often not everyone can grasp, debuglzq summed up a bit, there is no high-end technology, encountered a custom event of this thing, write down, the heart is not empty ~ The specific process may be 1000 people 1000 hamlet, see personal habits ~

For a custom CLR event in this article, for a custom WPF routed event, refer to:WPF Custom routed events

The next oral English should pay attention to exercise, technology to continue to study (focusing on strengthening LINQ,WCF,WPF and agile development Knowledge), and strive to improve their professional quality, during this time to complete the graduation thesis.

Time is very late ~ fingers on the keyboard wandering to the Dawn bar ~ What a beautiful night---so Beautiful!

=====================================================

Update:

We can use it. NET provides eventhandler<teventargs> delegate to simplify, define and use events, improve development efficiency and code quality ~

as follows (difference in steps 2 and 4, omitted 1):

Using System;namespace refactorcustomizeeventdemo{//HTTP://WWW.CNBLOGS.COM/DEBUGLZQ//Custom Event Step sample code//0. Defining parameters for event passing        Class Alarmeventargs:eventargs {public int numberofthief;        public AlarmEventArgs (int n) {numberofthief = n;        }}//The issuer of the event class Dog {//2. Declaring events public event eventhandler<alarmeventargs> Alarm;                3. The function that raised the event public void Onalarm (AlarmEventArgs e) {if (Alarm! = null) {                Console.WriteLine ("The Dog is Wangwang ...");            Alarm (this, e); }}}//Event Subscriber class Host {//4. Subscribe to Event public Host (dog dog) {//dog. Alarm + = new eventhandler<alarmeventargs> (hosthandleevent); This was equal to the next line dog.        Alarm + = hosthandleevent; }//5. event handler public void Hosthandleevent (Object sender, AlarmEventArgs e) {CONSOLE.WRiteline ("Host caught the thief!        The sum is "+ e.numberofthief); }} class Program {static void Main (string[] args) {Console.WriteLine ("Program is Runn            ing ... ");            Dog dog = new Dog ();            Host host = new host (dog);            Console.WriteLine ("Someone is coming ...");            6. Trigger Event AlarmEventArgs AlarmEventArgs = new AlarmEventArgs (3) at the appropriate time; Dog.            Onalarm (AlarmEventArgs);        Console.readkey (); }    }}

Step 2: The best practices for declaring events are as follows:

// 2. Declaring Events  Public Event Delegate { };

The program runs as follows:

Update:

Please refer to debuglzq follow-up blog:

WPF 4.5 New feature:using the WeakEvent pattern with WeakEventManager to avoid memory leaking

[C #] A simple implementation of the WeakEvent pattern

[Go] Custom events

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.