当前位置:网站首页>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!");
}
}
}
边栏推荐
- Sword finger offer03 Repeated numbers in the array [simple]
- Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
- Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
- 【ManageEngine】IP地址扫描的作用
- [ArcGIS user defined script tool] vector file generates expanded rectangular face elements
- Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
- 2021 autumn Information Security Experiment 1 (password and hiding technology)
- [comprehensive question] [Database Principle]
- [exercice 7] [principe de la base de données]
- Low code platform international multilingual (I18N) technical solution
猜你喜欢

Sword finger offer14 the easiest way to cut rope

Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)

基于同步坐标变换的谐波电流检测

Xctf mobile--rememberother problem solving

【数据挖掘复习题】

Two solutions of leetcode101 symmetric binary tree (recursion and iteration)

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

Analysis of the influence of voltage loop on PFC system performance

【R】【密度聚类、层次聚类、期望最大化聚类】

Differences between initial, inherit, unset, revert and all
随机推荐
如何在微信小程序中获取用户位置?
【综合题】【数据库原理】
Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
Sword finger offer14 the easiest way to cut rope
Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
Four problems and isolation level of MySQL concurrency
剑指Offer05. 替换空格
Sword finger offer09 Implementing queues with two stacks
【ManageEngine】IP地址扫描的作用
The latest version of lottery blind box operation version
Openstack node address change
alright alright alright
Swift bit operation exercise
Apache Mina开发手册
ImportError: No module named examples. tutorials. mnist
【習題五】【數據庫原理】
Grid connection - Analysis of low voltage ride through and island coexistence
Cache penetration and bloom filter
initial、inherit、unset、revert和all的区别
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?