ThreadPool.QueueUserWorkItem
我在一个程序里先创建线程池,然后子线程里又要创建线程,并使用线程池。
请问,他们使用的是不是同一个线程池?并且这样会有什么不好的后果?请指教。
问题点数:20、回复次数:13Top
1 楼toponly(花纯春)回复于 2004-12-02 10:16:25 得分 0
几分钟就沉到第二页了......Top
2 楼herony420(坦荡荡)回复于 2004-12-02 10:33:12 得分 0
是,每个进程只能具有一个操作系统线程池,线程池在首次创建 ThreadPool 类的实例时被创建。他们共同使用一个线程池,在子线程中创建新的线程并没有什么不好,这个管理是由操作系统来完成的,他有自己的算法,已经非常全面Top
3 楼hel(抵制日货,从我做起)回复于 2004-12-02 10:54:04 得分 0
上面说得很对,如果想使用多个线程池,也可以,参考:
http://www.codeproject.com/csharp/SmartThreadPool.aspTop
4 楼toponly(花纯春)回复于 2004-12-02 11:23:01 得分 0
谢谢两位的回答。
现在出现了这个错误:ThreadPool 对象中没有足够的自由线程来完成操作。
我一次增加100个任务,但只有23条顺利完成,其他都提示这个错误。Top
5 楼DotNetFreak()回复于 2004-12-02 11:28:48 得分 0
thread pool 默认值是25个thread
可以在mscoree.h 文件里改CorSetMaxThreadsTop
6 楼toponly(花纯春)回复于 2004-12-02 11:29:02 得分 0
没有可用线程的时候不是列队等待吗?怎么会这样。Top
7 楼hel(抵制日货,从我做起)回复于 2004-12-02 11:41:34 得分 0
确实TheadPool中默认只有20几个线程,应该是排队等待的,是你的代码出了问题。
最好把核心代码贴出来看看。Top
8 楼cometsky(找工作中,有意者CSDN短信联系)回复于 2004-12-02 11:47:49 得分 0
ThreadPool 只支持25个线程/CPUTop
9 楼toponly(花纯春)回复于 2004-12-02 11:51:21 得分 0
我做的很简单就是这样
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;
}Top
10 楼toponly(花纯春)回复于 2004-12-02 11:54:49 得分 0
hel(抵制日货,从我做起)提供的东西很好用,
但我想明白如何正确的使用线程池。Top
11 楼pellet(鱼丸)回复于 2004-12-02 12:03:06 得分 0
强力关注,我也想知道啊 .Top
12 楼toponly(花纯春)回复于 2004-12-02 14:31:12 得分 0
帮帮忙,帮我把问题解决好吗。
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);
}
}
}
Top
13 楼pellet(鱼丸)回复于 2004-12-02 15:00:33 得分 20
楼主看一下这段
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;
}
}Top




