. Net asynchronous programming

Source: Internet
Author: User

 I. How to understand asynchronous programming
Using. Net asynchronous programming, the. NET class method is called when the program continues to be executed until the specified callback is performed;
If callback is not provided, the call is blocked, polling, or waiting for completion.
For example, a program can call a method to enumerate a large list, and the main program will continue to execute. After completing the enumeration, callback is performed and the program will address it.
Ii. Use of asynchronous programming
File IO stream Io socket Io
Remote processing channels (HTTP, TCP) and Proxies
XML Web Services created using ASP. NET
ASP. NET web forms
Message Queue using the messagequeue class
Asynchronous Delegation
3. How to Make asynchronous calls?
. NET framework allows asynchronous calls to any method.
Define the delegate with the same signature for the method to be called
The Common Language Runtime automatically defines the begininvoke and endinvoke methods with the appropriate signature for the delegate.
Iv. Differences between delegation and traditional function pointers
Delegation is a safe and Object-Oriented function pointer. to deepen this understanding, C ++ implements a function pointer and C # implements a delegate respectively. See examples 1 and 2.
5. Introduction of delegate class, multicastdelegate and delegate keywords
The focus is on the invoke, begininvoke, and endinvoke methods and parameter meanings.
Vi. iasyncresult Interface
The iasyncresult interface is used to monitor and manage asynchronous operations. This interface is returned from the start operation and is passed to the end operation to associate the start operation with the end operation. If the callback is
If this parameter is specified as part of the start operation, asyncresult is passed to the callback. Its four attributes are as follows:
Asyncstate: returns the object provided as the last parameter in the start operation method call.
Asyncwaithandle: Return waithandle, which can be used to execute waithandle. waitone, waitany, or waitall.
Completedsynchronously: If the Start Operation call has been synchronized, The completedsynchronously attribute is set to true.
Iscompleted: After the server has processed the call, the iscompleted attribute is set to true.
VII. Four asynchronous programming methods
After begininvoke is called, there are four options:
(1) perform some operations and call endinvoke until the call is completed. (See example 3)
(2) Use iasyncresult. asyncwaithandle to get waithandle, and use its waitone method to block the execution until it is sent.
Waithandle signal, and then
Call endinvoke. (See example 4)
(3) poll the iasyncresult returned by begininvoke, determine when the asynchronous call is completed, and then call endinvoke. (See example 5)
(4) delegate the callback method to begininvoke. This method is executed on the threadpool thread after the asynchronous call is completed. It can call endinvoke. (See example 6.
)
Note: endinvoke is always called after the asynchronous call is complete.
8. Implementation of WebService asynchronous call, remote object asynchronous call, and asynchronous socket. The code is taken from msdn
9. Code
Example (1)
////////////////////////////////////////
# Include
Typedef int cfun (INT );
Void funtest (cfun * F, int I)
{
Cout <}
Int fun1 (int I)
{
Return I;
}
Int fun2 (int I, Int J)
{
Return I + J;
}
Int main ()
{
Funtest (cfun *) fun1, 5 );
Funtest (cfun *) fun2, 3); // unexpected results can be generated during runtime through compilation.
Return 0;
}
///////////////////////////////////////
Example (2)
///////////////////////////////////////
Using system;
Namespace delegatetest
{
Public Delegate int mydelegate1 (int I );
Public Delegate int mydelegate2 (int I, Int J );
Public Delegate int mydelegate3 (int I, Int J, int K );
Class classmain
{
[Stathread]
Static void main (string [] ARGs)
{
Classmain CM = new classmain ();
Mydelegate1 md1 = new mydelegate1 (CM. fun1 );
Int n = md1 (10 );
Console. writeline (N );
Mydelegate2 md2 = new mydelegate2 (CM. fun2 );
N = md2 (10,100 );
Console. writeline (N );
// Incorrect code. The delegate and function signature do not match and cannot be compiled
// MD = new mydelegate (CM. fun2 );
}
Private int fun1 (int I)
{
Return I;
}
Private int fun2 (int I, Int J)
{
Return I + J;
}
}
}
///////////////////////////////////////
Example (3)
///////////////////////////////////////
Using system;
Using system. Threading;
Namespace asytest1
{
Public Delegate void mydelegate ();
Class classmain
{
[Stathread]
Static void main (string [] ARGs)
{
Mydelegate MD = new mydelegate (fun );
Iasyncresult AR = md. begininvoke (null, null );
Int I = 1;
// Call the thread to simulate some operations in progress
While (I <1000000000)
{
I + = 2;
I-= 1;
}
Console. writeline ("the call thread is finished and the asynchronous call is completed ");
Md. endinvoke (AR );
Console. writeline ("program ended ");
}
Static void fun ()
{
// Simulate some time-consuming operations, such as retrieving files
Thread. Sleep (10000 );
Console. writeline ("Fun execution completed ");
}
}
}
//////////////////////////////////////
Example (4)
//////////////////////////////////////
Using system;
Using system. Threading;
Namespace asytest2
{
Public Delegate int mydelegate (int I, Int J );
Class classmain
{
[Stathread]
Static void main (string [] ARGs)
{
Mydelegate MD = new mydelegate (fun );
Iasyncresult AR = md. begininvoke (10, 20, null, null );
// Use the waiting handle
Ar. asyncwaithandle. waitone ();
Console. writeline ("wait for the end ");
Int K = md. endinvoke (AR );
Console. writeline ("program ended, K = {0}", k );
}
Static int fun (int I, Int J)
{
// Simulate some time-consuming operations, such as retrieving files
Thread. Sleep (10000 );
Console. writeline ("Fun execution completed ");
Return I + J;
}
}
}
//////////////////////////////////////
Example (5)
//////////////////////////////////////
Using system;
Using system. Threading;
Namespace asytest3
{
Public Delegate int mydelegate (int I, Int J );
Class classmain
{
[Stathread]
Static void main (string [] ARGs)
{
Mydelegate MD = new mydelegate (fun );
Iasyncresult AR = md. begininvoke (10, 20, null, null );
While (AR. iscompleted = false)
{
// Simulate other operations in progress
Console. writeline (".....");
Thread. Sleep (2000 );
}
Console. writeline ("End of polling ");
Int K = md. endinvoke (AR );
Console. writeline ("program ended, K = {0}", k );
}
Static int fun (int I, Int J)
{
// Simulate some time-consuming operations, such as retrieving files
Thread. Sleep (10000 );
Console. writeline ("Fun execution completed ");
Return I + J;
}
}
}
//////////////////////////////////////
Example (6)
//////////////////////////////////////
Using system;
Using system. Threading;
Namespace asytest4
{
Public Delegate int mydelegate (int I, Int J );
Class classmain
{
[Stathread]
Static void main (string [] ARGs)
{
Mydelegate MD = new mydelegate (fun );
// Start asynchronous call and specify the callback function callback
Iasyncresult AR = md. begininvoke (10, 20, new asynccallback (callback), MD );
// The main function continues to perform other operations
Thread. Sleep (5000 );
Console. writeline ("Main Function operation completed ");
// Prevent the primary function from ending before Fun
Console. Readline ();
}
Static int fun (int I, Int J)
{
// Simulate some time-consuming operations, such as retrieving files
Thread. Sleep (10000 );
Return I + J;
}
Static void callback (iasyncresult AR)
{
// Obtain the MD object reference in the main function
Mydelegate MD = (mydelegate) Ar. asyncstate;
Int K = md. endinvoke (AR );
Console. writeline ("Fun execution is complete, callback function execution, result = {0}", k );
}
}

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.