先模拟一个需要异步的函数,等待5秒
public class Math
{
public int Add(int i, int j)
{
Thread.Sleep(5000);
Console.WriteLine("计算完成!");
return i + j;
}
}
然后写一个委托
delegate int Sum(int a,int b);
Task做一个简单的泛型
static Task<int> GetSum(int a, int b)
{
Math mAdd = new Math();
return Task.Run<int>(() =>
{
return mAdd.Add(a, b);
}
);
}
然后在Main里调用
static void Main(string[] args)
{
HqlpSum();
Console.WriteLine("计算中。。。");
}
输出结果:
异步才符合做事标准-。-!
欢迎关注我的微信个人订阅号
我的微博:well火柴
每天多学一点0.0