????Tasks
????Task??Task<T>?????????????.NET Framework 4.0?У????Task?????????????????????????????????????????е????CPU????????????????IO???????????????????????????????????????????????
????static Task ReadFileAsync(string filePath??out byte[]buffer)
????{
????Stream stream=File.Open(filePath??FileMode.Open);
????buffer=new byte[stream.Length];
????var tcs=new TaskCompletionSource<double>();
????stream.BeginRead(buffer??0??buffer.Length??arr=>
????{
????var length=stream.EndRead(arr);
????tcs.SetResult(stream.Length);
????}??null);
????return tcs.Task;
????}
????????????????TaskCompletionSource??????????????????????Task?????????????????????????????????????Task???????????????
??????????????????Task????????Я???????????????????????await???????????????????
????static async void ReadAssignedFile()
????{
????byte[]buffer;
????try
????{
????double length=await ReadFileAsync("SomeFileDoNotExisted.txt"??out buffer);
????}
????catch(Exception ex)
????{
????Console.WriteLine(ex.Message);
????}
????}
????static Task<double>ReadFileAsync(string filePath??out byte[]buffer)
????{
????Stream stream=File.Open(filePath??FileMode.Open);
????buffer=new byte[stream.Length];
????var tcs=new TaskCompletionSource<double>();
????stream.BeginRead(buffer??0??buffer.Length??arr=>
????{
????try
????{
????var length=stream.EndRead(arr);
????tcs.SetResult(stream.Length);
????}
????catch(IOException ex)
????{
????tcs.SetException(ex);
????}
????}??null);
????return tcs.Task;
????}
????image

?????????????????????
?????????н?????????????????????-Task-based asynchronous Pattern(TAP)??????????????????????????÷?????????????????????????Task????Task<T>??
?????????н??????ЩTAP?е?????????????????“???”??“??????”??????????????????????????????
????Async??await
???????async??????????????????????????????????????д???async????????????κ?await?????????????????????????????????
????static async Task<int>TenToSevenAsync()
????{
????Thread.Sleep(10000);
????return 7;
????}
?????????????????????????????????10?????7?????????????????????VS??????????????棬????????????????????????
??????????async???????е????await????????????????????????????÷????????е??????????????????????????????????????ζ??????????async?????е???????????????????????????????á???????????????????????Ч??
????static async Task<int>TenToSevenAsync()
????{
????await Task.Delay(3000);
????return 7;
????}
????Task.Delay??????????汾??Tread??Sleep???????????Task?????Task??????????????????ɡ?
???????????????????????????
????????????????????????????await???????????????????????
??????????????У????????????????????????????????????????????????????????????????????????????????????????ɡ?????????????“???????”
?????????????????????????????????????“???????”-???void???????????Task<TResult>????????÷???????????????????????????????void saync??????????????Task????????????????????????????ɡ?
????private async void someButton_Click(object sender??RoutedEventArgs e)
????{
????someButton.IsEnabled=false;
????await SumPageSizesAsync(GetUrls()));
????someButton.IsEnabled=true;
????}
??????????
?????д???????????????????????