C # Delegate Asynchronous invocation

Source: Internet
Author: User

Reference page:

Http://www.yuanjiaocheng.net/webapi/mvc-consume-webapi-get.html

Http://www.yuanjiaocheng.net/webapi/mvc-consume-webapi-post.html

Http://www.yuanjiaocheng.net/webapi/mvc-consume-webapi-put.html

Http://www.yuanjiaocheng.net/webapi/mvc-consume-webapi-delete.html

Http://www.yuanjiaocheng.net/webapi/httpclient-consume-webapi.html

Nonsense not much to say, directly on the code (PS: I like simple, direct, rough)

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.Remoting.Messaging;
Using System.Text;
Using System.Threading;
Using System.Threading.Tasks;

Namespace calling a delegate asynchronously
{
Class Program
{
static void Main (string[] args)
{
MyDelegate MD = new mydelegate (sum);

#region General Program Invocation
int s = MD (1, 20);
Console.WriteLine ("Sum is: {0}", s);
#endregion

#region get the return value directly by calling the asynchronous committee, it blocks the main thread
Begins an asynchronous call, uses the thread pool,
IAsyncResult res = Md. BeginInvoke (1,30,null, "test");

There are other things you can do here.
Console.WriteLine ("Can do other things here, oh ~");

Getting the return value in this way interrupts the execution of the main thread
int s = Md. EndInvoke (RES);
Console.WriteLine ("Sum is: {0}", s);
#endregion

#region Implement the return value of an asynchronous delegate through a callback function without blocking the main thread
IAsyncResult res = Md. BeginInvoke (1, CallBack, "Test");
#endregion

Sub-thread you play slowly, I wait for you, while doing something, you do it, you go home together
int i = 0;
while (!res. iscompleted)
{
Console.WriteLine ("Main thread continues to execute ..." + (i++% 100). ToString ());
System.Threading.Thread.Sleep (100);
}

Console.readkey ();
}

Actual calculation-assumptions are time-consuming
private static int sum (int fromnum, int tonum)
{
int sum = 0;
for (int i = fromnum; i < tonum; i++)
{
sum++;
Thread.Sleep (100);
}
return sum;
}

callback function that calls this method when the thread ends asynchronously
private static void CallBack (IAsyncResult ar)
{
AsyncResult ar = ar as AsyncResult;
Console.WriteLine ("Callback parameter for the asynchronous call is {0}", AR.) asyncstate); Get and output the results of an asynchronous call
int s = ((MyDelegate) AR. AsyncDelegate). EndInvoke (AR);
Console.WriteLine ("Sum is: {0}", s);
}

}

Declaring a delegate
public delegate int MyDelegate (int fromnum, int tonum);
}

C # Delegate Asynchronous invocation

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.