Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace comprehensivetest.com
{
public class AsyCallEx112
{
Defines a delegate that performs an addition
public delegate int sum (int a, int b);
public class number
{
public int m = 4;
public int Numberadd (int a, int b)
{
int C = a + B;
return C;
}
Define a with. NET Framework-defined callback function for the AsyncCallback delegate
public void CallbackMothed2 (IAsyncResult ar2)
{
sum s = (sum) ar2. asyncstate;
int number = S.endinvoke (AR2);
m = number;
Console.WriteLine ("M value obtained: {0}", m);
}
}
}
}
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace comprehensivetest.com
{
public class AsyCallEx113
{
Define a delegate
public delegate void Asynceventhanlder ();
public class Class1
{
public void Event1 ()
{
Console.WriteLine ("Event1 start");
System.Threading.Thread.Sleep (2000);
Console.WriteLine ("Event1 End");
}
public void Event2 ()
{
Console.WriteLine ("Event2 start");
int i = 1;
while (I < 100)
{
i = i + 1;
Console.WriteLine ("Event2" + i.tostring ());
}
Console.WriteLine ("Event2 End");
}
public void Callbackmethod (IAsyncResult ar)
{
((Asynceventhanlder) ar. asyncstate). EndInvoke (AR);
}
}
}
}
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Comprehensivetest.com;
Namespace Comprehensivetest
{
Class Program
{
static void Main (string[] args)
{
112
Asycallex112.number num = new Asycallex112.number ();
Asycallex112.sum numberadd= New Asycallex112.sum (Num.numberadd);
AsyncCallback numberback = new AsyncCallback (Num. CALLBACKMOTHED2);
Numberadd. BeginInvoke (+, Numberback, numberadd);
Console.WriteLine ("Results obtained:");
Console.WriteLine (NUM.M);
113
Long start = 0;
Long end = 0;
Asycallex113.class1 C = new Asycallex113.class1 ();
Console.WriteLine ("Ready");
start = DateTime.Now.Ticks;
Asycallex113.asynceventhanlder asy = new Asycallex113.asynceventhanlder (C.EVENT1);
IAsyncResult ia = asy. BeginInvoke (null, NULL);
IAsyncResult ia = asy. BeginInvoke (New AsyncCallback (C.callbackmethod), asy);
Ia. Asyncwaithandle.waitone ();
C.event2 ();
end = DateTime.Now.Ticks;
Console.WriteLine ("Timescale difference =" + convert.tostring (End-start));
Console.ReadLine ();
}
}
}
This article is from the "Better_power_wisdom" blog, make sure to keep this source http://aonaufly.blog.51cto.com/3554853/1698947
C # Async callback function