监控线程池ThreadPool

监控线程池 运行完毕

int WorkItem = 0;
System.Threading.Semaphore hore;

在要循环插入的地方
//信号量 每次最多5个 可以不要这个
hore = new Semaphore(5, 5);
foreach(object o in object o2)


//计数
Interlocked.Increment(ref WorkItem);
ThreadPool.QueueUserWorkItem(
new WaitCallback(Registe), siteList);



//在开一个Thread去跑这个
private void WaitDone()
{

ThreadSafeInvoke(
this, delegate()
{
btnStart.Enabled
= false;
btnRef.Enabled
= false;

});



while (WorkItem > 0)
{
Thread.Sleep(
500);
}
MessageBox.Show(
"跑完了");




}

private void Registe(object obj)
{

hore.WaitOne();
try
        {

}
catch (Exception ex)
{


}
finally
{
hore.Release();
}



Interlocked.Decrement(
ref WorkItem);


}

原文地址:https://www.cnblogs.com/jcgh/p/2016910.html