是,每个进程只能具有一个操作系统线程池,线程池在首次创建 ThreadPool 类的实例时被创建。他们共同使用一个线程池,在子线程中创建新的线程并没有什么不好,这个管理是由操作系统来完成的,他有自己的算法,已经非常全面

解决方案 »

  1.   

    上面说得很对,如果想使用多个线程池,也可以,参考:
    http://www.codeproject.com/csharp/SmartThreadPool.asp
      

  2.   

    谢谢两位的回答。现在出现了这个错误:ThreadPool 对象中没有足够的自由线程来完成操作。我一次增加100个任务,但只有23条顺利完成,其他都提示这个错误。
      

  3.   

    thread pool 默认值是25个thread可以在mscoree.h 文件里改CorSetMaxThreads
      

  4.   

    确实TheadPool中默认只有20几个线程,应该是排队等待的,是你的代码出了问题。
    最好把核心代码贴出来看看。
      

  5.   

    ThreadPool 只支持25个线程/CPU
      

  6.   

    我做的很简单就是这样
    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;
    }
      

  7.   

    hel(抵制日货,从我做起)提供的东西很好用,
    但我想明白如何正确的使用线程池。
      

  8.   

    帮帮忙,帮我把问题解决好吗。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);
    }
    }
    }
      

  9.   

    楼主看一下这段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;
        }
    }