关于异步方法调用
using System;
namespace ConsoleApplication4
{
public delegate int BinaryOp(int x, int y, ref int z);
public class MathCode {
internal int sum = 0;
public int Add(int m, int n, ref int c) {
System.Threading.Thread.Sleep(5000);
c = m + n;
return c;
}
public int Subtract(int a, int b, ref int c) {
System.Threading.Thread.Sleep(5000);
c = a - b;
return c;
}
public int Multiplication(int a, int b, ref int c) {
System.Threading.Thread.Sleep(5000);
c = a * b;
return c;
}
public int Division(int a, int b, ref int c) {
System.Threading.Thread.Sleep(5000);
c = a / b;
return c;
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
MathCode target = new MathCode();
Type tt = typeof(MathCode);
Type dt = typeof(BinaryOp);
BinaryOp op1 = new BinaryOp(target.Division);
int objResult = 0;
IAsyncResult ar = op1.BeginInvoke(100, 10, ref objResult, null, null);
while (!ar.IsCompleted) {
System.Threading.Thread.Sleep(500);
Console.WriteLine("the caculation is still executing!!please wait!!!");
}
Console.WriteLine("this caculation is complete with the result of {0}.", objResult);
}
}
}
最后现实的objResult居然还是0,这是为什么?