zoukankan      html  css  js  c++  java
  • THREADSPOOL

      STPStartInfo stp = new STPStartInfo();//线程详细配置参数
                stp.CallToPostExecute = CallToPostExecute.Always;//在这里选择总是回调
                //当工作项执行完成后,是否释放工作项的参数,如果释放,参数对象必须实现IDisposable接口
                stp.DisposeOfStateObjects = true;
                //当线程池中没有工作项时,闲置的线程等待时间,超过这个时间后,会释放掉这个闲置的线程,默认为60秒
               // stp.IdleTimeout = 300;//300s
                //最大线程数,默认为25,
                //注意,由于windows的机制,所以一般最大线程最大设置成25,
                //如果设置成0的话,那么线程池将停止运行
                stp.MaxWorkerThreads = 50;//15 thread
                //只在STP执行Action<...>与Func<...>两种任务时有效
                //在执行工作项的过程中,是否把参数传递到WorkItem中去,用做IWorkItemResult接口取State时使用,
                //如果设置为false那么IWorkItemResult.State是取不到值的
                //如果设置为true可以取到传入参数的数组
                stp.FillStateWithArgs = true;
              
                //当工作项执行完毕后,默认的回调方法
                stp.PostExecuteWorkItemCallback = delegate(IWorkItemResult wir) { this.BeginInvoke(updateTxt, "-----------------ok" + wir.Result + "
    "); };
                //是否需要等待start方法后再执行工作项,?默认为true,当true状态时,STP必须执行Start方法,才会为线程分配工作项
                stp.StartSuspended = true;
    
                stp.AreThreadsBackground = true;
    
    
                m_hThreadPool = new SmartThreadPool(stp);//声明一个线程池
                
    
    
                foreach (int state in abc)
                {
                    
                    //IWorkItemResult<int> resultCallback = m_hThreadPool.QueueWorkItem(new Amib.Threading.Func<int, int>(IntDoSomeWork), state);
                   //m_hThreadPool.QueueWorkItem( (obj) =>
                   // {
                   //     Thread.Sleep(3000);
                   //     this.BeginInvoke(updateTxt, "正在执行" + state.ToString() + "
    ");
                   //     string str = "正在执行" + state.ToString() + "
    ";
                   //     return state * state;
                   // }, state);                
                    //this.BeginInvoke(updateTxt, resultCallback.Result.ToString() + "
    ");
                }
                m_hThreadPool.Start();
                m_hThreadPool.WaitForIdle();//等待该实例下的所有结果返回
                //MessageBox.Show(resultCallback.Result.ToString());
                m_hThreadPool.Shutdown();
  • 相关阅读:
    css侧边栏之综合实例3
    css侧边栏之综合实例2
    css实例之侧边栏
    css实例之正文
    css之使用 | margin | padding
    css之链接 | 点击后变色 | 悬停 | hover
    1.2(Mybatis学习笔记)Mybatis核心配置
    1.1(Mybatis学习笔记)初识Mybatis
    1.1(Spring学习笔记)Spring-事务基础
    1.4(Spring学习笔记)Spring-JDBC基础
  • 原文地址:https://www.cnblogs.com/wangchuang/p/4529003.html
Copyright © 2011-2022 走看看