当前位置:网站首页>C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
2022-07-03 12:52:00 【superfreak】
using System;
using System.Net;
using System.Diagnostics;
using System.Threading.Tasks;
namespace Chapter20AsyncSample_UsingAsyncP394
{
class MyDownloadingString
{
Stopwatch sw = new Stopwatch();// class System.Diagnostic.Stopwatch: Provides a set of methods and properties
//that you can use to accurately measure elapsed time.
public void DoRun()
{
const int LargeNumber = 6000000;
sw.Start();
// The following is the object to save the results
Task<int> t1 = CountCharactersAsync(1, "http://www.baidu.com");
Task<int> t2 = CountCharactersAsync(2, "http://www.csdn.net");
CountToALargeNumber(1, LargeNumber); CountToALargeNumber(2, LargeNumber);
CountToALargeNumber(3, LargeNumber); CountToALargeNumber(4, LargeNumber);
Console.WriteLine("Chars in http://www.baidu.com :{0}", t1.Result);// To get the results
Console.WriteLine("Chars in http://www.csdn.net :{0}", t2.Result);// To get the results
}
private async Task <int> CountCharactersAsync(int id, string uriString)
{
WebClient wc1 = new WebClient();// class System.Net.WebClient Provide a common method for obtaining information from resources ( use URI identification ) Send and receive data .
Console.WriteLine("Starting call {0}:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
// Below await It's a contextual keyword
string result = await wc1.DownloadStringTaskAsync(new Uri(uriString));// Download the resource as a string . Returns a string .
// Above is the use of task object , Specify as asynchronous operation
Console.WriteLine(" Call {0} completed:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
//Console.WriteLine("{0}", result);
return result.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)
{
MyDownloadingString ds = new MyDownloadingString();
ds.DoRun();
Console.WriteLine("Hello World!");
}
}
}
边栏推荐
- The latest version of lottery blind box operation version
- GCN thinking - word2vec directly calculates text classification
- Four problems and isolation level of MySQL concurrency
- 基于同步坐标变换的谐波电流检测
- 社交社区论坛APP超高颜值UI界面
- Quickly learn member inner classes and local inner classes
- Differences and connections between final and static
- 【习题七】【数据库原理】
- [exercise 5] [Database Principle]
- Oh my Zsh + TMUX installation
猜你喜欢

Sword finger offer06 Print linked list from end to end

Xctf mobile--rememberother problem solving

Sword finger offer10- I. Fibonacci sequence

【R】【密度聚类、层次聚类、期望最大化聚类】
![Sword finger offer03 Repeated numbers in the array [simple]](/img/cf/c1ad2f2a45560b674b5b8c11fed244.png)
Sword finger offer03 Repeated numbers in the array [simple]

Application of ncnn neural network computing framework in orange school orangepi 3 lts development board

Ali & ant self developed IDE

T430 toss and install OS majave 10.14

剑指Offer06. 从尾到头打印链表

TOGAF认证自学宝典V2.0
随机推荐
Oh my Zsh + TMUX installation
Apache Mina Development Manual
Sword finger offer06 Print linked list from end to end
阿里大于发送短信(用户微服务--消息微服务)
Exploration of sqoop1.4.4 native incremental import feature
[exercise 7] [Database Principle]
【習題七】【數據庫原理】
双链笔记·思源笔记综合评测:优点、缺点、评价
(最新版) Wifi分销多开版+安装框架
Redhat5 installing socket5 proxy server
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
4. 无线体内纳米网:电磁传播模型和传感器部署要点
Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
Swift Error Handling
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
CNN MNIST handwriting recognition
剑指Offer06. 从尾到头打印链表
Method overloading and rewriting
剑指Offer07. 重建二叉树