using System;
public delegate int AsyncMethod(int start,int end);//声明一个delegate
public class AsyncDemo
{
public static void Main()
{
AsyncMethod async = new AsyncMethod(DoSum);//实例化delegate
IAsyncResult result = async.BeginInvoke(1, 100000);//开始异步调用
Console.WriteLine("开始工作了。"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
int sum = async.EndInvoke(result);//停止结束异步调用
Console.WriteLine("结果是:" + sum);
Console.WriteLine("结束工作了。" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
public static int DoSum(int start, int end)
{
int result = 0;
for (int i = start; i < end; i++)
{
result += i;
}
return result;
}
}
public delegate int AsyncMethod(int start,int end);//声明一个delegate
public class AsyncDemo
{
public static void Main()
{
AsyncMethod async = new AsyncMethod(DoSum);//实例化delegate
IAsyncResult result = async.BeginInvoke(1, 100000);//开始异步调用
Console.WriteLine("开始工作了。"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
int sum = async.EndInvoke(result);//停止结束异步调用
Console.WriteLine("结果是:" + sum);
Console.WriteLine("结束工作了。" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
public static int DoSum(int start, int end)
{
int result = 0;
for (int i = start; i < end; i++)
{
result += i;
}
return result;
}
}