当前位置:网站首页>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!");
}
}
}
边栏推荐
- 【ManageEngine】IP地址扫描的作用
- [exercice 7] [principe de la base de données]
- Kotlin notes - popular knowledge points asterisk (*)
- Quick learning 1.8 front and rear interfaces
- [Exercice 5] [principe de la base de données]
- 【習題七】【數據庫原理】
- Powerful avatar making artifact wechat applet
- [data mining review questions]
- Xctf mobile--app2 problem solving
- 2021 autumn Information Security Experiment 1 (password and hiding technology)
猜你喜欢
Detailed explanation of the most complete constraintlayout in history
剑指Offer05. 替换空格
Record your vulnhub breakthrough record
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
How to get user location in wechat applet?
Xctf mobile--app3 problem solving
Cache penetration and bloom filter
最新版盲盒商城thinkphp+uniapp
如何在微信小程序中获取用户位置?
电压环对 PFC 系统性能影响分析
随机推荐
Swift return type is a function of function
idea将web项目打包成war包并部署到服务器上运行
Node.js: express + MySQL的使用
Apache Mina Development Manual
studio All flavors must now belong to a named flavor dimension. Learn more
【習題五】【數據庫原理】
Sword finger offer09 Implementing queues with two stacks
GCN thinking - word2vec directly calculates text classification
It feels great to know you learned something, isn‘t it?
【数据库原理复习题】
Idea packages the web project into a war package and deploys it to the server to run
Enable SASL authentication for memcached
剑指Offer07. 重建二叉树
Glide 4.6.1 API initial
十條職場規則
ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
A large select drop-down box, village in Chaoyang District
Quickly learn member inner classes and local inner classes
Sword finger offer10- I. Fibonacci sequence
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'