如何使线程等到事件发生并在ThreadPool.QueueUserWorkItem中继续其进程

时间:2014-08-19 10:50:15

标签: multithreading winforms c#-3.0

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。 每当线程池中的任何新线程启动时都应该等待事件发生,如果事件发生,它应该继续该过程。

0 个答案:

没有答案