Use Delegation for asynchronous programming

Source: Internet
Author: User

Asynchronous delegation provides the ability to call synchronous Methods asynchronously. When a delegate is synchronously called, the "Invoke" method calls the target method directly to the current thread. If the compiler supports asynchronous delegation, it generates the "Invoke" method and the "BeginInvoke" and "EndInvoke" methods. If you call the BeginInvoke method, the Common Language Runtime Library (CLR) queues requests and returns them to the caller immediately. This method will be called for threads from the thread pool. The original thread that submits the request freely continues to execute in parallel with the target method, which runs on the thread pool thread. If a callback method is specified in the call to the BeginInvoke method, the method is called when the target method returns. In the callback method, the "EndInvoke" method gets the return value and all input/output parameters. If no callback method is specified when you call BeginInvoke, you can call EndInvoke from the thread that calls BeginInvoke ".

With the user-specified delegate signature, the compiler should issue a delegate class with "Invoke", "BeginInvoke", and "EndInvoke" methods. The BeginInvoke and EndInvoke methods should be modified as local. Because these methods are marked as native, CLR automatically provides this implementation during class loading. Load programs to ensure they are not overwritten.

NET Framework allows you to call any method asynchronously. To this end, define the delegate with the same signature as the method you want to call. The Common Language Runtime automatically uses the appropriate signature to define the BeginInvoke and EndInvoke methods for the delegate.

The BeginInvoke method can be used to initiate asynchronous calls. It has the same parameters as the method to be executed asynchronously. It also has two optional parameters. The first parameter is an AsyncCallback delegate that references the method to be called when the asynchronous call is complete. The second parameter is a user-defined object that can pass information to the callback method. BeginInvoke returns immediately without waiting for the asynchronous call to complete. BeginInvoke returns IAsyncResult, which can be used to monitor the asynchronous call progress.

The EndInvoke method retrieves the result of an asynchronous call. After BeginInvoke is called, you can call the EndInvoke method at any time. If the asynchronous call is not completed yet, EndInvoke will stop the calling thread until the asynchronous call is completed. The EndInvoke parameters includeOutAndRefParameters (<Out> ByRef and ByRef in Visual Basic) and IAsyncResult returned by BeginInvoke.

 

1 using System;
2 using System. Collections. Generic;
3 using System. ComponentModel;
4 using System. Data;
5 using System. Drawing;
6 using System. Linq;
7 using System. Text;
8 using System. Windows. Forms;
9 using System. Threading;
10 using System. Run

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.