Just say the question.
Delegate
What is the class that defines what it is, in fact, a class, defining a delegate that defines a class? The class used to describe the type of the method. A field has a type, then the method actually has a type, that is, the delegate.
A delegate is a general definition of a class of methods.
Events
An event is the way a class is used to deliver a message. In addition to the event, the delegate is encapsulated as if the variable is a property.
OK, clear the concept, and then say the example.
Example
There are 3 object mouse, Button,winform. The mouse clicks on the button object, triggers the button's Click event, and WinForm captures the event and then processes it.
Meaning: button is a class, there is a click event, after it was clicked, sent a message to the outside "I was clicked", WinForm said "I will handle",WinForm How to capture it? Connect the bridges between them
is actually entrusted. WinForm registers its own method with the button's Click event through a class of delegates, and the method is invoked naturally when the event occurs.
It may not be appropriate to cite a real-world example. When we drive a car accident, traffic police will go to the scene to deal with, not fire or doctors or other people. Actually behind is a logic, predefined good, car accident is traffic police treatment, fire is fire treatment, illness is a doctor treatment.
Next, call asynchronously.
asynchronous calls and callbacks
As the name implies, Async is a different step, how to express the different steps?. NET implementation is to take a new thread to execute your method (don't know to understand right:))
Why async? Simply say WinForm bar, is not want the main thread is blocked by a long time execution method, resulting in a bad user experience, who also do not want to load the data form a pull, interface card dead, right.
How do you achieve asynchrony? The delegate is defined first. You're going to say why? Because there are BeginInvoke methods in the delegate class, I guess the implementation of the BeginInvoke method might contain a new thread, and then execute your method in the new thread that you need to execute asynchronously.
Well, the method begins to execute asynchronously. Well, how do I know it's done? Use callbacks.
You're going to ask what the callback is, huh? Callback is called back, then who is back? BeginInvoke opens the way for new threads to turn back and tune the main thread. What about the purpose? Notifies the main thread, "I'm done, you watch."
So how does it come true? My methods are delegates and events. The method of asynchronous invocation triggers a completed event after execution, which is defined in WinForm, and who handles it? The method in WinForm.
Next, put my code on. Simple example:)
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacewindowsformsapplication1{ Public Partial classForm1:form { Public Delegate voidProcesshandler (inti); PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { //process (); main thread direct call will block This. button1. Text ="loading in .... "; NewSystem.Threading.Thread (NewSystem.Threading.ThreadStart (process)). Start (); This. button1. text="Load Complete"; } Private voidprocess () { for(inti =0; I < -; i++) {System.Threading.Thread.Sleep ( -); setlist (i); } } Private voidSetlist (inti) {if( This. invokerequired) { This. BeginInvoke (NewProcesshandler (setlist),New Object[] {i}); return; } This. LISTBOX1.ITEMS.ADD (i); } Private voidButton2_Click (Objectsender, EventArgs e) {MessageBox.Show ("sssss"); } Private voidProcessref BOOLisfinished) { for(inti =0; I < -; i++) {System.Threading.Thread.Sleep ( -); setlist (i); } isfinished=true; Finished (refisfinished); } Public Delegate voidHeadprocess (ref BOOLisfinish); Public EventHeadprocess finished; Private voidButton3_Click (Objectsender, EventArgs e) { This. Button3. Text ="loading in .... "; BOOLisfinished =false; This. ListBox1.Items.Clear (); Headprocess HP=NewHeadprocess ( This. Process); IAsyncResult ia= HP. BeginInvoke (refIsfinished,NULL,NULL);
Finished+= This. setstatus;//How to handle registration completion events } Private voidSetStatusref BOOLisfinished) { if( This. invokerequired) { This. BeginInvoke (NewHeadprocess ( This. SetStatus),New Object[]{isfinished}]; return; } if(isfinished) { This. Button3. Text ="Load Complete"; MessageBox.Show ("Load Complete"); } Else { This. Button3. Text ="Load Failed"; } } } }
The first time to write a text, more rough, we make a look, the above explanations are my personal understanding, if not, please spit groove. :)
Talking about the probabilities of delegates, events, asynchronous calls, callbacks, etc.