TPL 异步调用高延迟操作

#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值