My understanding of the C # delegate has been vague. Today, I seem to have a bit of feeling. I want to record it. If not, please kindly advise.
Delegate-a class-level function pointer similar to a function. There are three key points: 1. Form-like function; 2. Class-level function; 3. function pointer, is a bridge that calls other functions. Below is Code Resolution:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Threading;
Namespace webbrowser
{
// 1. Declaration: the form is like a function, but the keyword delegate is added. Because it is a class level, its position is tied with other classes.
Delegate void dupdatestatus (string URL );
Public partial class formmain: Form
{
Thread trecord;
Public formmain ()
{
Initializecomponent ();
Trecord = new thread (New parameterizedthreadstart (record ));
}
Private void btrecord_click (Object sender, eventargs E)
{
Trecord. Start ();
}
Private void btstop_click (Object sender, eventargs E)
{
Trecord. Abort ();
}
Private void record (Object URL)
{
Url = "http://www.sina.com.cn/##.shtml ";
// 2. Delegate method: You can use the anonymous method to fill in or point to the named method.
Dupdatestatus du = new dupdatestatus
(
// Fill the delegate with the anonymous method. The input parameters must be consistent with those of the delegate.
Delegate (string urlcatch)
{
Tbstatus. Text + = urlcatch + "\ r \ n ";
}
);
// 3. Use: directly called or called
// Du (entry. url); // call directly. Because this thread is not a UI thread, an error is prompted.
This. begininvoke (DU, URL); // called by begininvoke to asynchronously update the UI
// This. Invoke (DU, entry. url); // it is called by invoke. Synchronously updating the UI will block this thread
}
}
}
}