async await
public static Task<double> GetValueAsync(double num1, double num2)
{
return Task.Run(() =>
{
Console.WriteLine(AppDomain.GetCurrentThreadId());
for (int i = 0; i < 1000000; i++)
{
num1 = num1 / num2;
}
return num1;
});
}
public async static void DisplayValue()
{
Console.WriteLine(AppDomain.GetCurrentThreadId());
double dd = await GetValueAsync(1234.5, 1.01);//此处会开新线程处理GetValueAsync任务,然后方法马上返回
//这之后的所有代码都会被封装成委托,在GetValueAsync任务完成时调用
// System.Diagnostics.Debug.WriteLine("Value is : " + result);
Console.WriteLine("Value is : " + dd);
}
static void Main(string[] args)
{
Console.WriteLine(AppDomain.GetCurrentThreadId());
DisplayValue(); //这里不会阻塞
Console.WriteLine("MyClass() End.");
Console.ReadKey();
}