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;
} //定义一个与.net framework定义的asynccallback委托相同的回调函数
public void CallbackMethod2(IAsyncResult ar2)
{
sum s = (sum)ar2.AsyncState;
int number = s.EndInvoke(ar2);
this.m = number;
}
}
static void Main(string[] args)
{
number num = new number();
sum numberadd = new sum(num.numberAdd);
AsyncCallback numberback = new AsyncCallback(num.CallbackMethod2);
numberadd.BeginInvoke(2, 9, numberback, numberadd);
Console.WriteLine(num.m);
Console.ReadLine();
}
BeginInvoke 这个是异步的啊 昏死
在你执行完之前就执行了这个了Console.WriteLine(num.m);
你在 Console.WriteLine(num.m);前加上
thread.sleep(100)
static void Main(string[] args) { number num = new number(); sum numberadd = new sum(num.numberAdd); AsyncCallback numberback = new AsyncCallback(num.CallbackMethod2); numberadd.BeginInvoke(2, 9, numberback, numberadd); thread.sleep(100); Console.WriteLine(num.m); Console.ReadLine(); }
可以了,谢了~~