CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

ThreadPool.QueueUserWorkItem

楼主toponly(花纯春)2004-12-02 09:56:37 在 .NET技术 / C# 提问

我在一个程序里先创建线程池,然后子线程里又要创建线程,并使用线程池。  
   
  请问,他们使用的是不是同一个线程池?并且这样会有什么不好的后果?请指教。 问题点数: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

相关问题

  • 关于threadPool
  • 在线等候,QueueUserWorkItem问题
  • 请教c#的threadPool是怎么用的?
  • 关于socket和threadpool的异步编程
  • 关于Socket通讯及线程池(ThreadPool)的问题?
  • 创建在ThreadPool中的线程如何在程序结束时终止
  • 用ThreadPool实现多线程,CPU成本为0,但进程一直在任务管理器里,WHY?

关键词

  • 线程
  • 代码
  • richtextbox
  • threadpool
  • oresponse
  • orequest
  • button
  • disposing
  • drawing
  • 使用

得分解答快速导航

  • 帖主:toponly
  • pellet

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo