public void CommandTypeProcess(List<XMLIdentifier> list, Command_Do ObjCommand_Do)
{
log.Info("[" + DateTime.Now + "]" + ":AddressTypeSplit class to process each unit in different thread");
var numThreads = 50;
var toProcess = numThreads;
var resetEvent = new ManualResetEvent(false);
//ObjCommand_Do = ObjGetMsgnumertorAndIMEIno.UnitIEMIMsgnumerator(ObjCommand_Do);
//ObjCommand_Do.Msgnumerator = ObjCommand_Do.Msgnumerator - 1;
//ObjCommand_Do.MsgnumeratorStart = ObjCommand_Do.Msgnumerator;
foreach(string unit in ObjCommand_Do.ListNumberOfunit)
{
if(unit!=Constant.NullValue)
{
log.Info("[" + DateTime.Now + "]" + "Thread is started for Unit"+unit);
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state)
{
// ObjCommandProcessThread.CommandProcessFunction(ObjCommand_Do,CommandPacket, CommandType, AddressUnitNumber,ObjCommand_Do.MsgnumeratorStart,ObjCommand_Do.MsgnumeratorEnd,AddressID);
ObjUnitDataPacketProcess.InputOutputPacketProcess(ObjCommand_Do, list,unit);
if (Interlocked.Decrement(ref toProcess) == 0) resetEvent.Set();
}), null);
resetEvent.WaitOne();
}
}
}
在上面的方法中,我为每个单元使用Threadpool.QueueUserWorkItem。 每当线程池中的任何新线程启动时都应该等待事件发生,如果事件发生,它应该继续该过程。