Valid C # item 21: Express callbacks with delegates

Source: Internet
Author: User
Valid C # item 21: Express callbacks with delegates

Dad: "Son, cut the grass in the yard. I will check the report later ."

Son: "Dad, I have cleaned the yard ."

Son: "Dad, I have added oil to the lawn mower ."

Son: "Dad, you can't start the lawn mower ."

Dad: "I will launch it ."

Son: "Dad, I have cut the grass ."

This is an example of callback processing. Dad assigned a task to his son and reported the progress of the task. When Dad waits for his son to finish his work, he does not block his ongoing activities. If your son has any important (or even unimportant) things, he can interrupt his father's activities to report the situation. Callback is used for asynchronous communication between the server and the client. This may include multi-threaded operations, or simply provides the entry point for Synchronous updates. Callback processing is implemented by delegation in C.

Delegation provides a secure definition for callback. Although we generally use delegation to handle events, it is not the only thing that can be used. When we need to configure communication between classes and want to implement loose coupling over interfaces, we can use delegation. Delegation allows us to configure the target during runtime and notify multiple clients. A delegate is an object that contains method references. By using delegation, we can communicate with one or more client objects and change their configurations during runtime.

Multi-channel broadcast delegation can trigger multiple functions added to the delegate when a function is called. For this method, we should pay attention to two points: first, it is not safe for exceptions, and secondly, its return value will be the return value of the last corresponding function.

In the response of a multi-channel broadcast delegate, each target must be successfully called. The delegate does not capture any related exceptions. Therefore, once a target throws an exception, the entire delegate response chain will be interrupted.

Similar problems also occur in the return value. We can define the return values for the delegate and check the returned values during callback:

Public Delegate bool continueprocessing ();

Public void lengthyoperation (continueprocessing Pred)
{
Foreach (complicatedclass CL in _ container)
{
C1.dolengthoperation ();
If (false = PRED ())
{
Return;
}
}
}

This is not a problem for a single delegate, but it is used in multi-channel broadcast delegation:

Continueprocessing CP = new continueprocessing (checkwithuser );
CP + = new continueprocessing (checkwithuser );
C. lengthyoperation (CP );

The Return Value of the delegate is the return value of the last function in the multi-channel broadcast delegate chain. All other return values are ignored, just like checkwithuser () in this example ().

We can manually call each delegate. Each delegate we create contains a list of delegates. We need to traverse this list to check the return values in the delegate chain.

Public Delegate bool continueprocessing ();

Public void lengthyoperation (continueprocessing Pred)
{
Bool bcontinue = true;
Foreach (complicatedclass CL in _ container)
{
C1.dolengthoperation ();
Foreach (continueprocessing PR in Pred. getinvocationlist ())
{
Bcountinue & = Pr ();
}
If (false = bcountinue)
{
Return;
}
}
}

In this way, the syntax defines that each delegate must be true.

The delegate provides the best way to Use callback during runtime and has no special requirements on the client. We can process the delegation target at runtime, and support multi-channel broadcast delegation. In. net, the client callback should be implemented using delegation.

Translated from Objective C #: 50 specific ways to improve your C # by Bill Wagner

Back to directory
 
For Delegation, I think of the Classic. Net bedtime story again...

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.