这是一段我程序中的下载网页的代码片断: // Create a new WebClient instance.
WebClient myWebClient = new WebClient();
label1.Text = "开始下载文件...";
byte[] myDataBuffer = myWebClient.DownloadData(remoteUrl);
//将buffer中的字节吗重新中文编码
string data = new string(System.Text.Encoding.GetEncoding("GB2312").GetChars(myDataBuffer));
label1.Text = "下载完毕!"下载一个网页很不错,但是遇到多个网页需要下载,比如说string remoteUrl[5],我尝试用for循环,运行时就会卡住请问怎么序列化下载这些网页好呢?

解决方案 »

  1.   

    WebClient还有一个方法是DownloadString下载东西要时间的,当然会“卡住”....你需要的是多线程。
      

  2.   

    可以直接异步下载。。不用自己管理线程System.Net.WebClient.DownloadStringAsync(System.Uri)System.Net.WebClient.DownloadStringCompleted如wc.DownloadStringAsync(new Uri(@"http://baidu.com"));
    wc.DownloadStringCompleted+=.....然后在事件里面处理
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    namespace ConsoleApplication1
    {
        class Program
        {
            static int ok=0;
            static void Main(string[] args)
            {
                string[] links = new string[] {"http://baidu.com","http://163.com","http://sohu.com" };
                foreach (string s in links)
                {
                    WebClient wc = new WebClient();
                    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                    wc.DownloadStringAsync(new Uri(s));
                }
                while (ok < 3) {
                    System.Threading.Thread.Sleep(100);//防止主线程结束程序
                }
            }        static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                Console.WriteLine(e.Result);
                ok++;
            }
        }
    }
      

  4.   

    那如果在winform下,一个button_Click事件中进行,每个结果输出到一字符串数组中该怎样做呢?
    对事件不是很熟悉~~~~
      

  5.   

    A多线程,B写在上一个的Complete事件里
      

  6.   

    终于找到方法了
    貌似BeginInvoke异步委托能解决~~
      

  7.   

    ding gang hao zai xue xi WebClient