STPStartInfo stp = new STPStartInfo();//线程详细配置参数
stp.CallToPostExecute = CallToPostExecute.Always;//在这里选择总是回调
//当工作项执行完成后,是否释放工作项的参数,如果释放,参数对象必须实现IDisposable接口
stp.DisposeOfStateObjects = true;
//当线程池中没有工作项时,闲置的线程等待时间,超过这个时间后,会释放掉这个闲置的线程,默认为60秒
// stp.IdleTimeout = 300;//300s
//最大线程数,默认为25,
//注意,由于windows的机制,所以一般最大线程最大设置成25,
//如果设置成0的话,那么线程池将停止运行
stp.MaxWorkerThreads = ;//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 + "\r\n"); };
//是否需要等待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() + "\r\n");
// string str = "正在执行" + state.ToString() + "\r\n";
// return state * state;
// }, state);
//this.BeginInvoke(updateTxt, resultCallback.Result.ToString() + "\r\n");
}
m_hThreadPool.Start();
m_hThreadPool.WaitForIdle();//等待该实例下的所有结果返回
//MessageBox.Show(resultCallback.Result.ToString());
m_hThreadPool.Shutdown();
05-11 22:55