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