本帖最后由 llsen 于 2010-09-14 12:26:10 编辑

解决方案 »

  1.   

    someEvent是事件,someHander是相应的委托。蓝色和红色字体没显示出来
      

  2.   

    delegate内部是用线程池调用的,不会存在你说的上百个上千个线程,所以不用担心 
      

  3.   

    或者直接用线程池,if ( someEvent!= null)
      {
      Delegate[] dels=someEvent.GetInvocationList();
      foreach (Delegate del in dels)
      {
          TheadPool.public static bool QueueUserWorkItem (delegate(){  someHander tim = (someHander)del;
        
      tim.BeginInvoke(dtTime, null, null);   }); //线程池  }
      }
      

  4.   

    According to MSDN:
    If the BeginInvoke method is called, the common language runtime (CLR) will 
    queue the request and return immediately to the caller. The target method 
    will be called on a thread from the thread pool.
      

  5.   

    不对,再更正下
    if ( someEvent!= null)
      {
      Delegate[] dels=someEvent.GetInvocationList();
      foreach (Delegate del in dels)
      {
          TheadPool.public static bool QueueUserWorkItem (delegate(){  someHander tim = (someHander)del;
        
      tim.Invoke(dtTime, null, null);   }); //因为是线程池了,所以就不用再Begin了,直接Invoke  }
      }
      

  6.   

    一个关于delegate和ThreadPool的讨论
    http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic61277.aspx
      

  7.   

    一个关于delegate和ThreadPool的讨论
    http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic61277.aspx
      

  8.   

    自己写个线程池管理吧,池子里的线程用完了就阻塞等待。工作线程完成任务了就release自己告诉线程池可以重新调用了
      

  9.   

    不会有成千上万的线程的,begininvoke是clr在线程池中获得可用线程来执行的,我不知道线程池中线程的数量,但是cpu*2是比较好的线程数。
    你可以在invoke函数中打印出CurrentThread.Id来获得threadid,看一下一共有多少个线程在执行。
      

  10.   

    这个问题也搞定了,只要这样即可:
    ThreadPool.SetMaxThreads(5, 5);谢谢大家的关注了,一会儿结贴。尤其谢谢bclz_vs的提醒!
      

  11.   

    Delegate[] dels=someEvent.GetInvocationList();在这之后判断 dels 的长度是否需要开启多线程,因为你准备开启5个线程,所以可以预设值大于50时才开启线程
    const int threadCount=5;bool flag = dels.Length>50;int[] threadArray=new int[0];
    if(flag)
    {
       threadArray=new int[dels.Length/threadCount -1];
       for(int i=1,len=threadArray.Length+1;i<len;i++)
       {
            threadArray[i-1]=threadCount*i+1;
       }
    }-----------------------------------------------------------------------
    if(!flag)
    {
     foreach (Delegate del in dels)
      {
           
      someHander tim = (someHander)del;
        
      tim.BeginInvoke(dtTime, null, null);
      }}
    else
    {
        //定义线程并实例化线程
        int index=1;
         foreach (Delegate del in dels)
         {              
             //do sth    
                  if(threadArray.Contains(index))
                  {
                     //让线程实例开启
                       //重新实例化线程实例
                  }
          }
    }
      

  12.   

    阿非兄弟我在BeginInvoke之前用的ThreadPool.SetMaxThreads(5, 5);设置了线程的数目,在另外一个我发的帖子中,有个哥们说这样会有问题的,但是没说什么问题,愁死我了。你能指点一下不?
    http://topic.csdn.net/u/20100914/11/06b8ca43-c391-4755-a692-990f8a5cd675.html?seed=623418642&r=68409230#r_68409230
    81楼
      

  13.   

    oracle数据库有个共享服务器的模式,它是处理大容量并发的,能不能参考下。