#region TPL 异步调用高延迟操作
//public class Program
//{
// public static Stopwatch Stopwatch = new Stopwatch();//定义一个计时器
// public static void Main(string[] args)
// {
// // Stopwatch.Start();
// string url = "https://www.cnblogs.com/tianqiang/p/9994516.html";//要访问的地址
// if (args.Length > 0)
// {
// url = args[0];
// }
// Console.WriteLine(url);
// Task task = WriteWebReauestSizeAsync(url);//委托一个线程执行这个方法,并且将地址传过去
// try
// {
// while (!task.Wait(100))//等待时间,单位是毫秒
// {
// Console.WriteLine(".");
// }
// }
// catch (AggregateException exception)//描述错误的消息
// {
// exception = exception.Flatten();
// // 将异常包装成一个实例,传递然后传递给他的父任务
// try
// {
// exception.Handle(innerException =>
// {
// ExceptionDispatchInfo.Capture(exception.InnerException).Throw(); return true;
// });
// }
// catch (WebException e)
// {
// Console.WriteLine("错误信息:{0}", e.Message);
// Console.WriteLine("堆栈信息:{0}", e.StackTrace);
// }
// catch (IOException e)
// {
// Console.WriteLine("错误信息:{0}", e.Message);
// Console.WriteLine("堆栈信息:{0}", e.StackTrace);
// }
// catch (NotSupportedException e)
// {
// Console.WriteLine("错误信息:{0}", e.Message);
// Console.WriteLine("堆栈信息:{0}", e.StackTrace);
// }
// }
// //Stopwatch.Stop();
// //Console.WriteLine("总时间{0} 毫秒", Stopwatch.ElapsedMilliseconds);
// }
// private static Task WriteWebReauestSizeAsync(string url)
// {
// StreamReader reader = null;
// WebRequest webRequest = WebRequest.Create(url);
// //ContinueWith 延续
// Task task = webRequest.GetResponseAsync().ContinueWith(antecedent =>
// {
// WebResponse respomse = antecedent.Result;
// reader = new StreamReader(respomse.GetResponseStream());
// return reader.ReadToEndAsync();
// }).Unwrap().ContinueWith(antecedent =>
// {
// if (reader != null) reader.Dispose();
// string text = antecedent.Result;
// Console.WriteLine(FormatBytes(text.Length));
// });
// return task;
// }
// static public string FormatBytes(long bytes)
// {
// string[] manitudes = new string[] { "GB", "MB", "KB", "Bytes" };
// long max = (long)Math.Pow(1024, manitudes.Length);//设置容量
// return string.Format("{1:##.##} {0}",//用字符串表示形式替换格式项的格式的副本即arg0和arg1。
// // public static String Format(String format, object arg0, object arg1);
// manitudes.FirstOrDefault(
// //FirstOrDefault 取序列中满足条件的第一个元素,如果没有元素满足条件,则返回默认值
// //还有一个 First 取序列中满足条件的第一个元素,如果没有元素满足条件, 抛出异常
// //linq 的方法,理解不深,回头补上
// manitude => bytes > (max /= 1024)) ?? "0 Bytes", (decimal)bytes / (decimal)max);
// //?? 是 C#2.0 中新增的运算符,可以认作三元运算符的简化版,
// // 起主要作用是 如果 ?? 运算符的左操作数非空,运算符就返回左操作符树,否则就返回右操作符数
// //相当于 if else
// }
//}
#endregion