你所不知道的C#: 非同步&執行緒
非同步的C#程式
一個同步的下載網頁程式
using System;
using System.Net;
namespace test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string content = downloadPage("https://yu-che-gao.github.io/steven-blog/");
            Console.WriteLine("網頁內容總共為 {0} 個字元。", content.Length);
            Console.ReadKey();
        }
        static string downloadPage(string url)
        {
            var webClient = new WebClient();  // 須引用 System.Net 命名空間。
            string content = webClient.DownloadString(url);
            return content;
        }
    }
}
考慮上面程式,上面程式利用WebClient的DownloadString()方法取得吾人部落格的原始碼,以下擷取重點程式。
var webClient = new WebClient();  // 須引用 System.Net 命名空間。
string content = webClient.DownloadString(url);
但可能會因為網路速度的不同,DownloadString方法執行時間會讓程式當在那裏。
