当前位置:网站首页>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!");
}
}
}
边栏推荐
- Dix règles de travail
- 剑指Offer09. 用两个栈实现队列
- Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
- 基于同步坐标变换的谐波电流检测
- How to stand out quickly when you are new to the workplace?
- 剑指Offer10- I. 斐波那契数列
- The solution to change the USB flash disk into a space of only 2m
- 初入职场,如何快速脱颖而出?
- RedHat5 安装Socket5代理服务器
- Cache penetration and bloom filter
猜你喜欢
自抗扰控制器七-二阶 LADRC-PLL 结构设计
最新版抽奖盲盒运营版
[ManageEngine] the role of IP address scanning
Xctf mobile--app1 problem solving
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
Grid connection - Analysis of low voltage ride through and island coexistence
The upward and downward transformation of polymorphism
如何在微信小程序中获取用户位置?
【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】
initial、inherit、unset、revert和all的区别
随机推荐
剑指Offer06. 从尾到头打印链表
Using swift language features, write a pseudo-random number generator casually
A large select drop-down box, village in Chaoyang District
Solve the problem of VI opening files with ^m at the end
Sword finger offer14 the easiest way to cut rope
Tensorflow binary installation & Failure
Harmonic current detection based on synchronous coordinate transformation
剑指Offer05. 替换空格
Nodejs+Express+MySQL实现登陆功能(含验证码)
[data mining review questions]
Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)
Differences between initial, inherit, unset, revert and all
【習題五】【數據庫原理】
Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
【习题五】【数据库原理】
initial、inherit、unset、revert和all的区别
Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】
并网-低电压穿越与孤岛并存分析
Oh my Zsh + TMUX installation