Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
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[] 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;
 }
Exemplo n.º 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 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;
 }
Exemplo n.º 4
0
 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);
 }
Exemplo n.º 5
0
        private static void DoPrivate(object state)
        {
            object[] array = state as object[];
            IList    list  = array[0] as IList;

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

            AsyncHelper.DataWithStateList dataWithStates = asyncContext.DataWithStates;
            Thread.CurrentThread.Name = "Thread " + AsyncHelper._threadNo;
            Interlocked.Increment(ref AsyncHelper._threadNo);
            string text = string.Concat(new object[]
            {
                Thread.CurrentThread.Name,
                "[",
                Thread.CurrentThread.ManagedThreadId,
                "]"
            });

            Trace.WriteLine("线程ID:" + text);
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (asyncContext.NeedBreak)
                    {
                        Trace.WriteLine("线程" + text + "未执行完跳出");
                        break;
                    }
                    object obj = list[i];
                    if (dataWithStates.IsWaitForData(obj, true))
                    {
                        if (asyncContext.NeedBreak)
                        {
                            Trace.WriteLine("线程" + text + "未执行完跳出");
                            break;
                        }
                        asyncContext.Exec(obj);
                        Trace.WriteLine(string.Format("线程{0}处理{1}", text, obj));
                    }
                }
            }
            if (asyncContext.NeedWaitAll)
            {
                while (dataWithStates.WaitForDataCount > asyncContext.ThreadCount)
                {
                    if (asyncContext.NeedBreak)
                    {
                        break;
                    }
                    object obj = dataWithStates.GetWaitForObject();
                    if (obj != null && dataWithStates.IsWaitForData(obj, false))
                    {
                        if (asyncContext.NeedBreak)
                        {
                            Trace.WriteLine("线程" + text + "未执行完跳出");
                            break;
                        }
                        asyncContext.Exec(obj);
                        Trace.WriteLine(string.Format("线程{0}执行另一个进程的数据{1}", text, obj));
                    }
                }
            }
            autoResetEvent.Set();
        }