private void Terminate() { // Finish thread by setting null loop body and signaling about available task LoopFunction = null; int workerThreadCount = this.workerThreads.Count; for (int i = 0; i < workerThreadCount; i++) { this.workerThreads[i].Terminate(); } }
/// <summary> /// Runs the ForEach loop. /// </summary> /// <param name="items">The items.</param> /// <param name="loopBody">The loop body.</param> public void DoForEach(IEnumerable <T> items, ForEachLoopDelegate loopBody) { this.enumerator = items.GetEnumerator(); this.LoopFunction = loopBody; // Signal waiting task to all threads and mark them not idle. for (int i = 0; i < this.threadCount; i++) { WorkerThread workerThread = workerThreads[i]; workerThread.ThreadIdle.Reset(); workerThread.TaskWaiting.Set(); } // Wait until all threads become idle for (int i = 0; i < this.threadCount; i++) { WorkerThread workerThread = workerThreads[i]; workerThread.ThreadIdle.WaitOne(); } }