帮帮忙,帮我把问题解决好吗。using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Threading; using System.IO; namespace WindowsApplication1 { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Button button1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 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() { this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(8, 8); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(776, 296); this.richTextBox1.TabIndex = 0; this.richTextBox1.Text = "richTextBox1"; // // button1 // this.button1.Location = new System.Drawing.Point(664, 320); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(120, 40); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(808, 381); this.Controls.Add(this.button1); this.Controls.Add(this.richTextBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { for (int i=0;i<50;i++) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.DownPage)); } } public void DownPage(object none) { string url = "http://www.guochao.com/"; HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(url); oRequest.Timeout = 1800000; HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse(); StreamReader sr = new StreamReader(oResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312")); string Contents = sr.ReadToEnd(); sr.Close(); oResponse.Close(); richTextBox1.AppendText(Contents); } } }
楼主看一下这段public class MyClass1 { SmartThreadPool _smartThreadPool = new SmartThreadPool(); public void DoAsyncWork(object state) { // Queue the work item IWorkItemResult wir = _smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoRealWork), state); // Do some other work here // Get the result of the operation object result = wir.GetResult(); } // Do the real work private object DoRealWork(object state) { object result = null; // Do the real work here and put the result in 'result' return result; } }
http://www.codeproject.com/csharp/SmartThreadPool.asp
最好把核心代码贴出来看看。
ThreadPool.QueueUserWorkItem(new WaitCallback(MyDown.Execute));
我通过一些用户界面方法得到一些网址并添加到数组里,定时查询这个数组,for()循环执行MyDown.Execute这样一个方法,下面这个是函数。就这么简单。
public static string DownPage(string url)
{
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(url);
oRequest.Timeout = 1800000;
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
StreamReader sr = new StreamReader(oResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
string Contents = sr.ReadToEnd();
sr.Close();
oResponse.Close();
return Contents;
}
但我想明白如何正确的使用线程池。
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Threading;
using System.IO;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 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()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(8, 8);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(776, 296);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "richTextBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(664, 320);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 40);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(808, 381);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
for (int i=0;i<50;i++)
{
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.DownPage));
}
} public void DownPage(object none)
{
string url = "http://www.guochao.com/";
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(url);
oRequest.Timeout = 1800000;
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
StreamReader sr = new StreamReader(oResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
string Contents = sr.ReadToEnd();
sr.Close();
oResponse.Close();
richTextBox1.AppendText(Contents);
}
}
}
{
SmartThreadPool _smartThreadPool = new SmartThreadPool(); public void DoAsyncWork(object state)
{
// Queue the work item
IWorkItemResult wir =
_smartThreadPool.QueueWorkItem(
new WorkItemCallback(this.DoRealWork),
state); // Do some other work here // Get the result of the operation
object result = wir.GetResult();
} // Do the real work
private object DoRealWork(object state)
{
object result = null; // Do the real work here and put the result in 'result' return result;
}
}