当前位置:网站首页>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!");
}
}
}
边栏推荐
- [comprehensive question] [Database Principle]
- 基于同步坐标变换的谐波电流检测
- Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
- Method overloading and rewriting
- Huffman coding experiment report
- 【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
- [data mining review questions]
- Swift5.7 extend some to generic parameters
- Enable SASL authentication for memcached
- idea将web项目打包成war包并部署到服务器上运行
猜你喜欢

Sword finger offer07 Rebuild binary tree

(latest version) WiFi distribution multi format + installation framework

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

Brief introduction to mvcc

studio All flavors must now belong to a named flavor dimension. Learn more

Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)

4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment

阿里大于发送短信(用户微服务--消息微服务)

Grid connection - Analysis of low voltage ride through and island coexistence

电压环对 PFC 系统性能影响分析
随机推荐
Differences and connections between final and static
阿里 & 蚂蚁自研 IDE
Low code platform international multilingual (I18N) technical solution
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
Do you feel like you've learned something and forgotten it?
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)
[exercise 6] [Database Principle]
(最新版) Wifi分销多开版+安装框架
Kotlin notes - popular knowledge points asterisk (*)
Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
【习题五】【数据库原理】
Enter the length of three sides of the triangle through the user, and calculate the area of the triangle, where the length is a real number
Apache Mina开发手册
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
[judgment question] [short answer question] [Database Principle]
【习题六】【数据库原理】
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
Eureka self protection