??????????
??????????????.net?????е??????ε??????NET ??C# ??????汾?????????????“????”??????C#1.0?й????→C#2.0????→C#3.0LINQ→C#4.0???????→C#4.5?????
???????????????????????????????????? .NET Framework 4.5 ?? Windows ??????????????? ??????????п???????????е??????????????ó??????????????????????????????? ????????????С??????????????????????к??????https://msdn.microsoft.com/zh-cn/library/hh191443.aspx??
??????ν?????????????CPU?????????????????????????Task??Task???await??????????????????????????????????????????????????????λ????????????????????н???
??????????????
?????????????????? Async ?? async ???η???
???????????????????????????“Async”?????β??
?????????????????????????
??????????????в?????? TResult ?????????????? Task??
???????????????з????????????в???????????????? Task??
????Sub in Visual Basic) if you’re writing an async event handler.”>??????д??????????????????? Void??Visual Basic ??? Sub????
?????й???????????μ???????????“????????????”??
?????????????????????? await ??????????????????????????????????????????????????????? ?????????????????????????????????÷???????????ν????????????????????????????????????÷?????????
???????????
?????????????????????????
C#
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
namespace ?????
{
class Program
{
static void Main(string[] args)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
ConsoleAsync1();
stopwatch.Stop();
Console.WriteLine("????????????" + stopwatch.ElapsedMilliseconds);
stopwatch.Reset();
stopwatch.Start();
ConsoleAsync();
stopwatch.Stop();
Console.WriteLine("???????????"+ stopwatch.ElapsedMilliseconds);
Console.Read();
}
private static async void ConsoleAsync()
{
Console.WriteLine("?????????");
Console.WriteLine("Result:" + await SumAsync(10));
Console.WriteLine("??????????");
}
private static async Taskint> SumAsync(int part)
{
if ((part += 10) >= 100)
{
return 100;
}
HttpClient client = new HttpClient();
Taskstring> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
Console.WriteLine(DateTime.Now.Millisecond + " ?? " + (await getStringTask).Length);
return await SumAsync(part);
}
private static void ConsoleAsync1()
{
Console.WriteLine("??????????");
Console.WriteLine("Result:" + SumAsync1(10));
Console.WriteLine("???????????");
}
private static int SumAsync1(int part)
{
if ((part += 10) >= 100)
{
return 100;
}
HttpClient client = new HttpClient();
Taskstring> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
Console.WriteLine(DateTime.Now.Millisecond + " ??? " + getStringTask.Result.Length);
return SumAsync1(part);
}
}
}
????????????
????1??????????????????????????1??????????????????2??????????????????
????2???????????????????飬???????????????????????????????????????£?????????Ч?????????????Ч??
????3????δ????е?GetStringAsync???????????????????????????????????????????
?????????????£?

????????????
????1????????????????У????????ɡ?
????2?????????????????????7??????й?????????????
???????β??
?????????????????????????????????????????????????????????????????????????????????????????????