当前位置:网站首页>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!");
}
}
}
边栏推荐
- Solve the problem of VI opening files with ^m at the end
- Swift Error Handling
- 【ManageEngine】IP地址扫描的作用
- 剑指Offer09. 用两个栈实现队列
- Xctf mobile--app1 problem solving
- The best shortcut is no shortcut
- A large select drop-down box, village in Chaoyang District
- 最新版盲盒商城thinkphp+uniapp
- Openstack node address change
- Social community forum app ultra-high appearance UI interface
猜你喜欢

Sword finger offer14 the easiest way to cut rope

【综合题】【数据库原理】

initial、inherit、unset、revert和all的区别

Leetcode234 palindrome linked list

ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
![[data mining review questions]](/img/96/00f866135e06c4cc0d765c6e499b29.png)
[data mining review questions]

剑指Offer09. 用两个栈实现队列

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

Alibaba is bigger than sending SMS (user microservice - message microservice)

Quick learning 1.8 front and rear interfaces
随机推荐
It feels great to know you learned something, isn‘t it?
Nodejs+Express+MySQL实现登陆功能(含验证码)
Apache Mina Development Manual
Day 1 of kotlin learning: simple built-in types of kotlin
Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
RedHat5 安装Socket5代理服务器
idea将web项目打包成war包并部署到服务器上运行
双链笔记·思源笔记综合评测:优点、缺点、评价
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
Eureka self protection
Openstack node address change
Apache Mina开发手册
[exercise 7] [Database Principle]
Social community forum app ultra-high appearance UI interface
十條職場規則
node的ORM使用-Sequelize
社交社区论坛APP超高颜值UI界面
[review questions of database principles]
阿里大于发送短信(用户微服务--消息微服务)
Idea packages the web project into a war package and deploys it to the server to run