C # understanding of events and methods for customizing events

Source: Internet
Author: User

Understanding of events:

During the development of the skyline project, I encountered a new knowledge: event.

In the program, I want to implement a function to obtain the coordinates of the viewpoint in real time when the viewpoint coordinates move. Here we need to use the concept of event: When the coordinates move, a specific event is triggered, and a signal can be sent, you can customize a function (the parameter must be the same as the parameter entrusted by the event, which is explained later). When it sends a signal, I can execute this function.

For example, A and B are friends. They met in the morning. B and A said, "Let me know after dinner at noon today. I will take you to the internet cafe to play.

In this situation, Party A has no way to control the meal. He can only send a signal after a meal to take a to the online version. Of course, Party B can stay at the door of Party A's house and watch Party A. After Party A's meal, Party A can take him to an internet cafe. But is this a waste of time.

Similarly, in the skyline project, we can create an infinite loop to record the parameters of the viewpoint coordinates at any time, then we get the viewpoint coordinate parameter next time in the loop and compare it with the previous one. If it is different, we will execute the function we want to run. In this case, the program will form an endless loop, which is meaningless.

Let's write a small program to embody the story of Party A and Party B:

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace sample001 {Public Delegate void informhandle (Object sender); public class Jia {public event informhandle eatover; Public void eat () {console. writeline ("eating ...... "); system. threading. thread. sleep (2000); // two-second oneating () for a meal event; // This is equivalent to a signal, which is sent when the function is run.} Public Virtual void oneating () {If (eatover! = NULL) {eatover (this) ;}} public class Yi {public void takejiatowangba (Object sender) {console. writeline ("with" + sender. tostring () + "go to the Internet cafe! ") ;}} Class program {static void main (string [] ARGs) {Jia jia1 = new Jia (); Yi yi1 = new Yi (); jia1.eatover + = new informhandle (yi1.takejiatowangba); console. writeline ("idle"); console. writeline ("do not know what to do now"); jia1.eat (); console. writeline ("I went to the internet cafe all night till noon the next day"); jia1.eat (); console. readkey ();}}}

Running result



The significance of this Code is that the takejiatowangba method of Party B will only be executed after Party A's meal. When we write

jia1.EatOver += new InformHandle(yi1.TakeJiaToWangBa);

This line of statement indicates the table name. When a triggers the eatover event, a signal is sent, and B starts to execute the takejiatowangba function. In the subsequent code, we don't have to worry about when to execute the takejiatowangba method.


Another problem is how does a trigger the eatover event, which is accomplished by the following code. when running the Eat method, a executes the oneating method. I am not very familiar with the principles of the following code.

        public virtual void OnEating()        {            if (EatOver != null)            {                EatOver(this);            }        }

The eatover event is triggered when the oneatring method is executed. The eatover (this) signal has been sent out. The signal also contains a parameter (the instance itself ). If someone else wants to run the code they want to run after the event is triggered, define a method, this method must also have a parameter of the same type as this event (in this Code, their types are all objects ), the user-defined method can access this parameter during running.

What determines the type of this parameter? It is determined by delegation!

    public delegate void InformHandle(object sender);


I am not very clear about it. It probably means this. I will modify this article after I make it clear. If you have any questions about this article or want to give me some advice, you can post comments. I will read them every day. Thank you!

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.