Understanding of C # Delegation

Source: Internet
Author: User
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
}
}
}
}

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.