Negative tive C # item21: Delegate expression callback

Source: Internet
Author: User

Callback is used to provide asynchronous feedback between the server and the client, which may involve multithreading or require an entry point for synchronous update. in C #, we use a delegate to express the callback.

Delegation provides a type-safe callback definition. Although most common delegated applications are related to events, it is not the whole scenario of delegated applications. When classes need to communicate with each other and we expect a more loosely coupled mechanism than interfaces, delegation is the most appropriate choice. The delegate allows us to configure the target at runtime and notify multiple objects. The delegate object contains a method reference. This method can be a static method or an instance method.

We can also bind multiple methods to the delegate and use the multicast mechanism of the delegate to call multiple methods at a time. Note: 1) if a delegate call encounters an exception, this construction cannot be ensured; 2) the return value of the whole multicast call is the return value of the last called method.

In the process of a multicast delegate call, each target is called sequentially, and the delegate object itself does not capture any exceptions. Therefore, any exceptions thrown by the target end the call of the delegate chain.

Similarly, the return value of the delegate has the same problem. If the return value type of the delegate is not void, the final return value of a multicast delegate is the last method executed on the delegate chain, other returned values are ignored.

Let's take a look at the following code.

Code

 public delegate bool ContinueProcessing();

public void LengthyOperation( ContinueProcessing pred )
{
foreach( ComplicatedClass cl in _container )
{
cl.DoLengthyOperation();
// Check for user abort:
if (false == pred())
return;
}
}


//Test
ContinueProcessing cp = new ContinueProcessing (
CheckWithUser );
cp += new ContinueProcessing( CheckWithSystem );
c.LengthyOperation( cp );

The preceding Code ignores the return value of the checkwithuser () method during execution.

We can manually traverse the delegate chain to solve this problem. Let's look at the following code.

Code

  1 public delegate bool ContinueProcessing();
2
3  public void LengthyOperation( ContinueProcessing pred )
4 {
5 bool bContinue = true;
6 foreach( ComplicatedClass cl in _container )
7 {
8 cl.DoLengthyOperation();
9 foreach( ContinueProcessing pr in
10 pred.GetInvocationList( ))
11
12 bContinue &= pr();
13
14 if (false == bContinue)
15 return;
16 }
17 }

In the above Code, we call the getinvocationlist () method to manually traverse the delegate chain, which can solve the problem that the returned value of the checkwithuser () method mentioned above is ignored.

 

Delegation provides us with the best way to perform callback at runtime. This method only has simple requirements for the customer class, and we can configure the delegation target at runtime. In addition, delegation also supports multicast. In. net, we should use delegation to implement callback.

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.