当前位置:网站首页>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!");
}
}
}
边栏推荐
- Public and private account sending prompt information (user microservice -- message microservice)
- Swift return type is a function of function
- [data mining review questions]
- Swift bit operation exercise
- alright alright alright
- 4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
- Sword finger offer04 Search in two-dimensional array [medium]
- 阿里大于发送短信(用户微服务--消息微服务)
- Attack and defense world mobile--ph0en1x-100
- Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
猜你喜欢
![Sword finger offer04 Search in two-dimensional array [medium]](/img/c4/002c951f8d914aaea4f4133685ebd1.png)
Sword finger offer04 Search in two-dimensional array [medium]

【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素

强大的头像制作神器微信小程序

The upward and downward transformation of polymorphism

【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)

Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?

How to convert a decimal number to binary in swift

4. 无线体内纳米网:电磁传播模型和传感器部署要点

剑指Offer05. 替换空格

Detailed explanation of the most complete constraintlayout in history
随机推荐
The latest version of lottery blind box operation version
启用MemCached的SASL认证
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
剑指Offer10- I. 斐波那契数列
T430 toss and install OS majave 10.14
【综合题】【数据库原理】
十条职场规则
Sword finger offer14 the easiest way to cut rope
[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
Swift Error Handling
Keep learning swift
How to stand out quickly when you are new to the workplace?
Integer case study of packaging
Sword finger offer06 Print linked list from end to end
Public and private account sending prompt information (user microservice -- message microservice)
【R】【密度聚类、层次聚类、期望最大化聚类】
剑指Offer06. 从尾到头打印链表
Apache Mina Development Manual
[exercice 7] [principe de la base de données]