What is the use of custom things?

Source: Internet
Author: User

Note: For a long time, I am confused about what custom events and methods do in the class.
What is the difference? Why do events need to be used to call functions implemented by methods? As a XX on csdn says, it's too much to fart with your pants off ~~ Well, I don't agree, since Microsoft can make this thing
There must be a certain degree of existence. It is strange that we are still superficial in learning. Today, I visited the Internet and finally learned some of the principles. The special record is as follows!

Suppose we want to implement a function: Define a class in advance, and this class is executing
When designing this class, we do not know who is going to call the method, therefore, it is impossible to write the method of the previous caller during method execution (1
This method is called the callback method ). Of course, if you do not adopt the principle of delegate + event, this function will not be implemented well (at least I do not know how to implement it yet, do you need to define an interface and then reflect it?
Very troublesome !), Therefore, the delegate + Event method can be used.

First, define a logical implementation class. The task of this class must implement logical functions and notify the caller of its status at any time. The specific implementation is as follows:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Threading;

Namespace consoleapplication1
{
/// <Summary>
/// Define the delegate first. This is indispensable. As for the parameter, You can implement the function at will. Here we need to use the parameter to implement the notification. Therefore, a string type is defined.
/// </Summary>
/// <Param name = "info"> </param>
Public Delegate void eventhandle (string info );
Class class1
{
/// <Summary>
/// Define the event. You need to bind the callback Method to the caller. The notification effect can be achieved only when the callback method is available!
/// </Summary>
Public event eventhandle eventhandletest;

/// <Summary>
/// This method assumes that the logic is implemented.
/// </Summary>
Public void start ()
{
If (eventhandletest! = NULL)
{
Thread. Sleep (1000); // suppose a logical code is executed here.

//Key points:
The above-defined event is used to call the callback method of the "Caller ~! The purpose of the notification is here. For example, in winform development, you need to update
Label tells her where to execute it here (of course, multithreading). You need to specify a method to call the program class here for the traditional method ~ That is to say, you must know the called party
And then use the delegate + Event method, you no longer know who is calling this method ~ As long as it is bound with a callback method, the notification can be accurate! Therefore, code duplication is added.
Usability! The notification information is transmitted based on parameters.
Eventhandletest ("completion progress: 20% ");

Thread. Sleep (1000); // suppose a logical code is executed here.
Eventhandletest ("completion progress 40%"); // executes the callback notification

Thread. Sleep (1000); // suppose a logical code is executed here.
Eventhandletest ("completion progress 60%"); // executes the callback notification

Thread. Sleep (1000); // suppose a logical code is executed here.
Eventhandletest ("completion progress 80%"); // executes the callback notification

Thread. Sleep (1000); // suppose a logical code is executed here.
Eventhandletest ("completion progress 100%"); // executes the callback notification

}

}
}
}

The code for the day before yesterday is as follows:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Timers;
Namespace consoleapplication1
{
Class Program
{

Static void main (string [] ARGs)
{
Class1 c = new class1 ();
// If you use the start method in class1 to implement functions and add notifications, You need to bind the callback function in advance.
C. eventhandletest + = new eventhandle (c_eventhandletest );
// After the callback function is bound, you can call the method.
C. Start ();
Console. Read ();
}

/// <Summary>
///
This is the callback method. By passing the parameter -- that is, the notification information, you can know how the start method you are calling is actually executed! Of course
In the process development mode, you may also need this, invoke + delegate method to update the control text on the main thread
/// </Summary>
/// <Param name = "info"> </param>
Static void c_eventhandletest (string info)
{
// Throw new notimplementedexception ();
Console. writeline (Info );
}
}
}

Note: compared with the use of timer, it is still a bit confusing.
If I want to call the start method and get the notification, I need to bind the callback function before calling the start method.
You can call the start method or enbled method before or after the binding method! This is still quite confusing, so I have time to understand it.
~~

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.