Two threads get the source code of the web page (2)

xiaoxiao2021-03-06  42

I have visited the bookstore. I found a book mentioned. One phenomenon. Just the download page, because the main thread is used. Then, it will cause the program window to move the inconvenience. I feel. I mentioned the download. Use another thread. So start learning multithreading. After the program is good, the window will not be caught when there is a big difference. And the content is also flashing out .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;

Summary description of Namespace BanyiWeb {///

/// Form1. /// /// 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; /// // / / The designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // TOD Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// 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 = "Please enter the URL:"; /// 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 sys Tem.drawing.Point (344, 8); this.button1.name = "button1"; this.button1.size = new system.drawing.size (56, 20); this.button1.tabindex = 2; this.button1 .Text = "Submit"; 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 = "Please enter the URL correctly, slower when the program is not found,"; /// // 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 = page Source code "; this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [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)); // initializes a thread instance mythread.Start (); // start threads} public void UpdateMyList (string str) {richTextBox1.Text = str;} public class getweb {public updateList ul; public string webstr; Public void web () {ix ("http:") <0) {MessageBox.show ("Error Banyi", "Your URL is incorrect"); Return;} httpwebresponse res; httpwebRequest Req; Try {req = (httpwebrequest) WebRequest.create (Webstr); res = (httpwebresponse) req.getResponse ();} catch (exception) {MessageBox.show ("Error Banyi", "I can't find this URL") Return;} streamreader strm = new streamreader (res. maxponsestream (), encoding.getencoding (54936)); // Simplified Chinese Encoding.Getencoding (54936), Encoding.ascii String Sline; String S Line2; sline2 = ""; do // learning notes do loop C # do while cycler loop DO Until cyclic body loop {sline = Strm.readline (); sline2 = sline2 sline;} while (sline! = Null); STRM .Close (); ul (sline2);}}}}

转载请注明原文地址:https://www.9cbs.com/read-56144.html

New Post(0)