using System;
using System.Net;
using System.Diagnostics;
namespace Chapter20AsyncSample_WithoutUsingAsyncP394
{
class MyDownloadingString
{
Stopwatch sw=new Stopwatch();//类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();
int t1 = CountCharacters(1,"http://www.baidu.com");
int t2 = CountCharacters(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);
Console.WriteLine("Chars in http://www.csdn.net :{0}", t2);
}
private int CountCharacters(int id,string uriString)
{
WebClient wc1 = new WebClient();//类System.Net.WebClient 提供通用的方法用于从资源处(用URI标识)发送和接收数据。
Console.WriteLine("Starting call {0}:{1,4:N0} ms",id,sw.Elapsed.TotalMilliseconds);
string result = wc1.DownloadString(new Uri(uriString));//将资源以字符串形式下载下来。返回一个字符串
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!");
}
}
}