二线程获取网页的源码(2)

类别:.NET开发 点击:0 评论:0 推荐:

逛了一下书店.发现了有一本书提到.的一个现象.就是下载网页的时候由于用的是主线程这样.然后就会造成程序窗口移动的不方便.卡住的感觉.提到了下载可以用到另外的一个线程.于是就开始学习多线程了.
程序好了后果然和原来的有很大的差别下载的时候窗口不会卡住了.而且内容也是一下就闪了出来.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Text;
using System.Threading;
using System.IO;

namespace Banyiweb
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 ///
 public delegate void UpdateList (string str) ;
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox textBoxWeb;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.StatusBar statusBar1;
  private System.Windows.Forms.RichTextBox richTextBox1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TOD 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.label1 = new System.Windows.Forms.Label();
   this.textBoxWeb = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.statusBar1 = new System.Windows.Forms.StatusBar();
   this.richTextBox1 = new System.Windows.Forms.RichTextBox();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(0, 16);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(72, 16);
   this.label1.TabIndex = 0;
   this.label1.Text = "请输入网址:";
   //
   // textBoxWeb
   //
   this.textBoxWeb.Location = new System.Drawing.Point(80, 8);
   this.textBoxWeb.Name = "textBoxWeb";
   this.textBoxWeb.Size = new System.Drawing.Size(248, 21);
   this.textBoxWeb.TabIndex = 1;
   this.textBoxWeb.Text = "http://www.";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(344, 8);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(56, 20);
   this.button1.TabIndex = 2;
   this.button1.Text = "提交";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // statusBar1
   //
   this.statusBar1.Location = new System.Drawing.Point(0, 351);
   this.statusBar1.Name = "statusBar1";
   this.statusBar1.Size = new System.Drawing.Size(416, 22);
   this.statusBar1.TabIndex = 5;
   this.statusBar1.Text = "http://www.banyi.com/txxl  by: banyi";
   //
   // richTextBox1
   //
   this.richTextBox1.Location = new System.Drawing.Point(0, 32);
   this.richTextBox1.Name = "richTextBox1";
   this.richTextBox1.Size = new System.Drawing.Size(416, 320);
   this.richTextBox1.TabIndex = 6;
   this.richTextBox1.Text = "请正确输入网址,在程序找不到网址的时候会变慢";
   //
   // Form1
   //
   this.AutoScale = false;
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(416, 373);
   this.Controls.Add(this.richTextBox1);
   this.Controls.Add(this.statusBar1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.textBoxWeb);
   this.Controls.Add(this.label1);
   
   this.Name = "Form1";
   this.Text = "网页源代码";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void button1_Click(object sender, System.EventArgs e)
  { 
   getweb getweb1 = new getweb();
   getweb1.webstr=textBoxWeb.Text;
   Thread  mythread;
   getweb1.ul= new UpdateList(UpdateMyList);
   mythread = new Thread ( new ThreadStart (getweb1.web) ) ; //初始化一个线程实例
   mythread.Start ( ) ; //启动线程
  }
  public void UpdateMyList(string str)
  {
   richTextBox1.Text=str;
  }
  public class getweb
  {
   public UpdateList ul;
   public string webstr;
   public void web()
   {
    if (webstr.IndexOf("http:") <0)
    {
     MessageBox.Show("出错了 banyi","你输入的网址有误");
     return;
    }
   
    HttpWebResponse res;
    HttpWebRequest req;
    try
    {
     req=(HttpWebRequest)WebRequest.Create(webstr);
     res=(HttpWebResponse)req.GetResponse();
    }
    catch (Exception)
    {
     MessageBox.Show("出错了 banyi","对不起找不到该网址");
    
     return;
    }
    
    StreamReader strm=new StreamReader(res.GetResponseStream(),Encoding.GetEncoding(54936));//简体中文 Encoding.GetEncoding(54936),Encoding.ASCII
    string sLine;
    string sLine2;
    sLine2="";
   
    do  //学习笔记do循环C# do while 循环体 loop  do until 循环体 loop
    {
     sLine=strm.ReadLine();
     sLine2=sLine2+sLine;
    }
    while (sLine!=null);
    strm.Close();
     ul(sLine2);
    
   }
  }

 }
}

 

本文地址:http://com.8s8s.com/it/it41756.htm