public void Run(IWorkItemCollection work_items) { if (semaphore == null) { Initialize(); } if (runSingleThreaded || threads.Count == 0) { for (int i = 0; i < work_items.Count; i++) { work_items.InternalDoWorkItem(i); } } else { workerThreadCount = threads.Count; nextWorkIndex = -1; workItems = work_items; Thread.MemoryBarrier(); semaphore.Release(threads.Count); manualResetEvent.WaitOne(); manualResetEvent.Reset(); if (errorOccured) { foreach (WorkerThread thread in threads) { thread.PrintExceptions(); } } } }
public bool DoNextWorkItem() { int num = Interlocked.Increment(ref nextWorkIndex); if (num < workItems.Count) { workItems.InternalDoWorkItem(num); return(true); } return(false); }