private static void DoPrivate(object state)
        {
            object[] objArray = state as object[];
            IList    list     = objArray[0] as IList;

            AsyncHelper.AsyncContext asyncContext   = objArray[1] as AsyncHelper.AsyncContext;
            AutoResetEvent           autoResetEvent = objArray[2] as AutoResetEvent;

            AsyncHelper.DataWithStateList dataWithStates = asyncContext.DataWithStates;
            Thread.CurrentThread.Name = "Thread " + (object)AsyncHelper._threadNo;
            Interlocked.Increment(ref AsyncHelper._threadNo);
            string str = Thread.CurrentThread.Name + "[" + (object)Thread.CurrentThread.ManagedThreadId + "]";

            Trace.WriteLine("线程ID:" + str);
            if (list != null)
            {
                for (int index = 0; index < list.Count; ++index)
                {
                    if (asyncContext.NeedBreak)
                    {
                        Trace.WriteLine("线程" + str + "未执行完跳出");
                        break;
                    }
                    object obj = list[index];
                    if (dataWithStates.IsWaitForData(obj, true))
                    {
                        if (asyncContext.NeedBreak)
                        {
                            Trace.WriteLine("线程" + str + "未执行完跳出");
                            break;
                        }
                        asyncContext.Exec(obj);
                        Trace.WriteLine(string.Format("线程{0}处理{1}", (object)str, obj));
                    }
                }
            }
            if (asyncContext.NeedWaitAll)
            {
                while (dataWithStates.WaitForDataCount > asyncContext.ThreadCount && !asyncContext.NeedBreak)
                {
                    object waitForObject = dataWithStates.GetWaitForObject();
                    if (waitForObject != null && dataWithStates.IsWaitForData(waitForObject, false))
                    {
                        if (asyncContext.NeedBreak)
                        {
                            Trace.WriteLine("线程" + str + "未执行完跳出");
                            break;
                        }
                        asyncContext.Exec(waitForObject);
                        Trace.WriteLine(string.Format("线程{0}执行另一个进程的数据{1}", (object)str, waitForObject));
                    }
                }
            }
            autoResetEvent.Set();
        }
 private static void DoAsyncPrivate(IList dataCollection, int threadCn, WaitCallback processItemMethod, DoGetObjTask getObjMethod, bool needWaitAll, bool hasReturnValue, out Hashtable processResult)
 {
     if (dataCollection == null)
     {
         throw new ArgumentNullException("dataCollection");
     }
     if (threadCn >= 64 || threadCn < 2)
     {
         throw new ArgumentOutOfRangeException("threadCn", "threadCn 参数必须在2和64之间");
     }
     if (threadCn > dataCollection.Count)
     {
         threadCn = dataCollection.Count;
     }
     IList[] listArray = (IList[])new ArrayList[threadCn];
     AsyncHelper.DataWithStateList dataWithStates = new AsyncHelper.DataWithStateList();
     AutoResetEvent[] autoResetEventArray         = new AutoResetEvent[threadCn];
     for (int index = 0; index < threadCn; ++index)
     {
         listArray[index]           = (IList) new ArrayList();
         autoResetEventArray[index] = new AutoResetEvent(false);
     }
     for (int index1 = 0; index1 < dataCollection.Count; ++index1)
     {
         object data   = dataCollection[index1];
         int    index2 = index1 % threadCn;
         listArray[index2].Add(data);
         dataWithStates.Add(new AsyncHelper.DataWithState(data, AsyncHelper.ProcessState.WaitForProcess));
     }
     AsyncHelper.AsyncContext context = AsyncHelper.AsyncContext.GetContext(threadCn, dataWithStates, needWaitAll, hasReturnValue, processItemMethod, getObjMethod);
     for (int index = 0; index < threadCn; ++index)
     {
         ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncHelper.DoPrivate), (object)new object[3]
         {
             (object)listArray[index],
             (object)context,
             (object)autoResetEventArray[index]
         });
     }
     if (needWaitAll)
     {
         WaitHandle.WaitAll((WaitHandle[])autoResetEventArray);
     }
     else
     {
         WaitHandle.WaitAny((WaitHandle[])autoResetEventArray);
         context.SetBreakSignal();
     }
     processResult = context.ProcessResult;
 }
Example #3
0
 private static void DoAsyncPrivate(IList dataCollection, int threadCn, WaitCallback processItemMethod, DoGetObjTask getObjMethod, bool needWaitAll, bool hasReturnValue, out Hashtable processResult)
 {
     if (dataCollection == null)
     {
         throw new ArgumentNullException("dataCollection");
     }
     if (threadCn >= 64 || threadCn < 2)
     {
         throw new ArgumentOutOfRangeException("threadCn", "threadCn 参数必须在2和64之间");
     }
     if (threadCn > dataCollection.Count)
     {
         threadCn = dataCollection.Count;
     }
     IList[] array = new ArrayList[threadCn];
     AsyncHelper.DataWithStateList dataWithStateList = new AsyncHelper.DataWithStateList();
     AutoResetEvent[] array2 = new AutoResetEvent[threadCn];
     for (int i = 0; i < threadCn; i++)
     {
         array[i]  = new ArrayList();
         array2[i] = new AutoResetEvent(false);
     }
     for (int j = 0; j < dataCollection.Count; j++)
     {
         object obj = dataCollection[j];
         int    num = j % threadCn;
         array[num].Add(obj);
         dataWithStateList.Add(new AsyncHelper.DataWithState(obj, AsyncHelper.ProcessState.WaitForProcess));
     }
     AsyncHelper.AsyncContext context = AsyncHelper.AsyncContext.GetContext(threadCn, dataWithStateList, needWaitAll, hasReturnValue, processItemMethod, getObjMethod);
     for (int k = 0; k < threadCn; k++)
     {
         ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncHelper.DoPrivate), new object[]
         {
             array[k],
             context,
             array2[k]
         });
     }
     if (needWaitAll)
     {
         WaitHandle.WaitAll(array2);
     }
     else
     {
         WaitHandle.WaitAny(array2);
         context.SetBreakSignal();
     }
     processResult = context.ProcessResult;
 }
 public static AsyncHelper.AsyncContext GetContext(int threadCn, AsyncHelper.DataWithStateList dataWithStates, bool needWaitAll, bool hasReturnValue, WaitCallback processItemMethod, DoGetObjTask hasReturnValueMethod)
 {
     AsyncHelper.AsyncContext asyncContext = new AsyncHelper.AsyncContext();
     asyncContext.ThreadCount    = threadCn;
     asyncContext.DataWithStates = dataWithStates;
     asyncContext.NeedWaitAll    = needWaitAll;
     if (hasReturnValue)
     {
         Hashtable hashtable = Hashtable.Synchronized(new Hashtable());
         asyncContext.ProcessResult        = hashtable;
         asyncContext.HasReturnValueMethod = hasReturnValueMethod;
     }
     else
     {
         asyncContext.VoidMethod = processItemMethod;
     }
     asyncContext.HasReturnValue = hasReturnValue;
     return(asyncContext);
 }