C # simple steps for using Delegation

Source: Internet
Author: User

C # simple steps for using Delegation

The delegate can be understood as the function pointer in C or C ++. Calling the delegate actually calls the delegate method.

The procedure for using delegation is as follows:

1. Define Delegation

// Define the delegate use keyword delegate
Private delegate void SetProgressBarValueDelegate (int value );

2. Declare Delegation

Private SetProgressBarValueDelegate setProgressBarValue;

3. instantiate the delegate

SetProgressBarValue = new SetProgressBarValueDelegate (SetProgressBarValue1 );

SetProgressBarValue1 is the name of the delegate method. The parameter type must be consistent with the delegate type. The prototype is as follows:

// Set progress bar 1
Private void SetProgressBarValue1 (int value)
{
PgProgressBar1.Value = value;
}

4. Delegated use:

SetProgressBarValueMethod (setProgressBarValue );

// Set the progress bar value
Private void SetProgressBarValueMethod (SetProgressBarValueDelegate setProgressBarValueDelegate)
{
For (int I = 1; I <= 100; I ++)
{
Application. DoEvents ();
Thread. Sleep (50 );
SetProgressBarValueDelegate (I );
}
}



/* This Command tells the system to continue processing other events on the user interface to avoid false positives.
* Equivalent to Visual Basic 6.0 DoEvents ()
* Processing events () in easy language ()*/
Application. DoEvents ();
/* This command is used to suspend a thread (parameter: millisecond)
* If it is used in the main thread, it will lead to a false death. You can only pause it to make it clearer */

Thread. Sleep (50 );

Zookeeper

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.