c# 线程池多任务处理并返回值
2021-06-08 18:03
标签:class als reading eset count 操作 threading llb ack 先新建一个封装类 包含 manual和返回值 using System; public void ReturnThreadData(object obj) //------------------------------------------------------------------------------------------------- List c# 线程池多任务处理并返回值 标签:class als reading eset count 操作 threading llb ack 原文地址:https://www.cnblogs.com/luozhai714/p/14522309.html
using System.Threading;
public class ThreadReturnData
{
public ManualResetEvent manual;
public string res;
{
//操作方法
res = DoSomething(obj);
manual.Set();
}
}
IList
for (int i = 0; i {
ThreadReturnData temp = new ThreadReturnData();
temp.manual = new ManualResetEvent(false);
arrManual.Add(temp.manual);
ThreadPool.QueueUserWorkItem(new WaitCallback(temp.ReturnThreadData), i);
testList.Add(temp);
}
}
if (arrManual.Count > 0)
{
////等待所有线程执行完
WaitHandle.WaitAll(arrManual.ToArray());
}
foreach (ThreadReturnData d in testList)
{
d.res;
//dowork
}