当前位置:网站首页>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!");
}
}
}
边栏推荐
- 剑指Offer06. 从尾到头打印链表
- Kotlin notes - popular knowledge points asterisk (*)
- [exercise 5] [Database Principle]
- 【判断题】【简答题】【数据库原理】
- Sword finger offer06 Print linked list from end to end
- [combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
- Apache Mina Development Manual
- 2021 autumn Information Security Experiment 1 (password and hiding technology)
- Grid connection - Analysis of low voltage ride through and island coexistence
- Approve iPad, which wants to use your icloud account
猜你喜欢

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

Xctf mobile--rememberother problem solving

Solve the problem of VI opening files with ^m at the end

Huffman coding experiment report

Node. Js: use of express + MySQL
![Sword finger offer04 Search in two-dimensional array [medium]](/img/c4/002c951f8d914aaea4f4133685ebd1.png)
Sword finger offer04 Search in two-dimensional array [medium]

Sword finger offer10- I. Fibonacci sequence

2021 autumn Information Security Experiment 1 (password and hiding technology)

GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈

The latest version of blind box mall thinkphp+uniapp
随机推荐
Grid connection - Analysis of low voltage ride through and island coexistence
Swift5.7 extend some to generic parameters
Kotlin notes - popular knowledge points asterisk (*)
Eureka自我保护
[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)
基于同步坐标变换的谐波电流检测
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
Idea packages the web project into a war package and deploys it to the server to run
Nodejs+express+mysql realizes login function (including verification code)
Low code platform international multilingual (I18N) technical solution
【习题七】【数据库原理】
Airflow installation jump pit
Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
node的ORM使用-Sequelize
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
Xctf mobile--rememberother problem solving
[exercise 6] [Database Principle]
并网-低电压穿越与孤岛并存分析
公纵号发送提示信息(用户微服务--消息微服务)