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; }
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); }
public static void DoAsync(IList dataCollection, int threadCn, DoGetObjTask processItemMethod, out Hashtable processResult) { AsyncHelper.DoAsyncPrivate(dataCollection, threadCn, (WaitCallback)null, processItemMethod, true, true, out processResult); }
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 i = 0; i < dataCollection.Count; i++) { object obj = dataCollection[i]; int num = i % 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 i = 0; i < threadCn; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncHelper.DoPrivate), new object[] { array[i], context, array2[i] }); } if (needWaitAll) { WaitHandle.WaitAll(array2); } else { WaitHandle.WaitAny(array2); context.SetBreakSignal(); } processResult = context.ProcessResult; }