博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#异步编程
阅读量:5052 次
发布时间:2019-06-12

本文共 5025 字,大约阅读时间需要 16 分钟。

在异步编程前,先举个没有异步的示例:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Diagnostics;// 7 using System.Net;// 8  9 namespace ConsoleApplication610 {11     class MyDownloadString12     {13         Stopwatch sw = new Stopwatch();//14 15         public void DoRun()16         {17             const int LargeNumber = 6000000;18             sw.Start();19 20             int t1 = CountCharacters(1, "http://www.cnblogs.com");21             int t2 = CountCharacters(2, "http://www.163.com");22 23             CountToALargeNumber(1, LargeNumber);24             CountToALargeNumber(2, LargeNumber);25             CountToALargeNumber(3, LargeNumber);26             CountToALargeNumber(4, LargeNumber);27 28             Console.WriteLine("Chars in http://www.microsoft.com          :{0}", t1);29             Console.WriteLine("Chars in http://www.illustratedcsharp.com  :{0}", t2);30         }31 32         private int CountCharacters(int id, string uriString)33         {34             WebClient wc1 = new WebClient();35 36             Console.WriteLine("Starting call {0}    :    {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);//开始下载网页时间37             string result = wc1.DownloadString(new Uri(uriString));//正式下载网页数据38             Console.WriteLine("Call {0} completed   :    {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);//完成网页下载39             //Console.WriteLine(result);40             return result.Length;41         }42 43         private void CountToALargeNumber(int id, int value)44         {45             for (long i = 0; i < value; i++) ;46 47             Console.WriteLine("End counting {0}     :   {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);//一个循环结束时间,目的是延时48 49         }50     }51     class Program52     {53         static void Main(string[] args)54         {55             MyDownloadString ds = new MyDownloadString();56             ds.DoRun();57             Console.ReadKey();58         }59 60     }61 }62 //output:63 //------------------------------------------------------------------------64 //Starting call 1    :       1 ms65 //Call 1 completed:        207 ms66 //Starting call 2    :     207 ms67 //Call 2 completed:        382 ms68 //End counting 1  :    424 ms69 //End counting 2  :    447 ms70 //End counting 3  :    469 ms71 //End counting 4  :    491 ms72 //Chars in http://www.microsoft.com          :4117773 //Chars in http://www.illustratedcsharp.com  :6870
View Code

 异步

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Diagnostics;//using System.Net;//namespace ConsoleApplication1{    class MyDownloadString    {        Stopwatch sw = new Stopwatch();        public void DoRun()        {            const int LargeNumber = 6000000;            sw.Start();            Task
t1 = CountCharactersAsync(1, "http://www.163.com"); Task
t2 = CountCharactersAsync(2, "http://www.qq.com"); CountToALargeNumber(1, LargeNumber); CountToALargeNumber(2, LargeNumber); CountToALargeNumber(3, LargeNumber); CountToALargeNumber(4, LargeNumber); Console.WriteLine("Chars in http://www.164.com : {0}", t1.Result); Console.WriteLine("Chars in http://www.qq.com : {0}", t2.Result); } private async Task
CountCharactersAsync(int id, string site) { WebClient wc = new WebClient(); Console.WriteLine("String call {0} : {1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds); string result1 = await wc.DownloadStringTaskAsync(new Uri(site)); Console.WriteLine("Call {0} completed : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds); return result1.Length; } private void CountToALargeNumber(int id, int value) { for (long i = 0; i < value; i++) ; Console.WriteLine("End counting {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds); } } class Program { static void Main(string[] args) { MyDownloadString ds = new MyDownloadString(); ds.DoRun(); Console.ReadKey(); } }}/*String call 1 : 2 msString call 2 : 160 msEnd counting 1 : 261 msEnd counting 2 : 283 msEnd counting 3 : 309 msEnd counting 4 : 334 msCall 1 completed : 353 msChars in http://www.164.com : 6870Call 2 completed : 954 msChars in http://www.qq.com : 599258---------------------------------------------------------------------Starting call 1 : 1 msCall 1 completed : 255 msStarting call 2 : 255 msCall 2 completed : 751 msEnd counting 1 : 785 msEnd counting 2 : 808 msEnd counting 3 : 830 msEnd counting 4 : 852 msChars in http://www.microsoft.com :6870Chars in http://www.illustratedcsharp.com :599274*/

 

转载于:https://www.cnblogs.com/wanghaibin/p/4937373.html

你可能感兴趣的文章
【知识库】-数据库_MySQL 的七种 join
查看>>
.net 写文件上传下载webservice
查看>>
noSQL数据库相关软件介绍(大数据存储时候,必须使用)
查看>>
iOS开发——缩放图片
查看>>
HTTP之URL的快捷方式
查看>>
满世界都是图论
查看>>
配置链路聚合中极小错误——失之毫厘谬以千里
查看>>
代码整洁
查看>>
蓝桥杯-分小组-java
查看>>
Java基础--面向对象编程1(类与对象)
查看>>
Android Toast
查看>>
iOS开发UI篇—Quartz2D使用(绘制基本图形)
查看>>
docker固定IP地址重启不变
查看>>
桌面图标修复||桌面图标不正常
查看>>
JavaScript基础(四)关于对象及JSON
查看>>
关于js sort排序方法
查看>>
JAVA面试常见问题之Redis篇
查看>>
javascript:二叉搜索树 实现
查看>>
网络爬虫Heritrix源码分析(一) 包介绍
查看>>
__int128的实现
查看>>