本帖最后由 zhang308337299 于 2012-02-27 21:51:22 编辑

解决方案 »

  1.   

    static void ChangeControlValues<T>(T control, Action<T> action)
                 where T : Control
            {
                if (control.InvokeRequired)
                {
                    control.Invoke(action, control);
                }
                else
                {
                    action(control);
                }
            }Thread td;void btn_Click(....)
    {
      string sourceUrl=....;
      td=new Thread(url=>{
      //你代码中的那段TRY CATCH
      //其中包含更改主线程上控件信息的改为使用最上面的静态方法替代。
      //exp: 
     ChangeControlValues(richTextBox1,r=>{r.Text = pageHtml;});
    }),sourceUrl);
    }
      

  2.   

    PS:我上面忘了把td.Start()了。
      

  3.   

    你可以只download网页中你想要获取的那一部分代码
    而没必要下载所有
      

  4.   

    class Form1
    {
      static void ChangeControlValues<T>(T control, Action<T> action) where T : Control  
      {
        if (control.InvokeRequired)
        {
          control.Invoke(action, control);
        }
        else
        {
          action(control);
        }
      }
      
      private void Btn_Click(object sender,EventArgs e)
      {
        Btn.Enable=false;
        Thread td=new Thread(_=>{
          try
          { 
            WebClient MyWebClient = new WebClient();        MyWebClient.Credentials = CredentialCache.DefaultCredentials;        Byte[] pageData = MyWebClient.DownloadData("http://www.baidu.com");        string pageHtml = Encoding.UTF8.GetString(pageData); 
            ChangeControlValues(richTextBox1,r=>{r.Text=pageHtml;});
          }
          catch (WebException webEx)
         {
           ChangeControlValues(richTextBox1,r=>{r.Text=webEx.Message.ToString();});
         }
         finally
         {
           ChangeControlValues(Btn,b=>{b.Enable=true;});   
         }
        },null);
       td.Start();
      }
    }