public void SetUpAndProcess(bool threading, int simulatedProcessTimeForItem) { int totalItems = 100; ItemToProcess[] startvalues = new ItemToProcess[totalItems]; for (int i = 0; i < totalItems; i++) { startvalues[i] = new ItemToProcess(); startvalues[i].startValue = i; startvalues[i].processingTime = simulatedProcessTimeForItem; } if (threading) { ProcessItemsThreaded(startvalues); } else { ProcessItemsNonThreaded(startvalues); } // It's vital we wait for all the child threads to finish before we continue this thread. // The main thead could close before it's children. WaitForProcessingToFinish(totalItems, startvalues); Trace.WriteLine("All Done!"); }
private static void WaitForProcessingToFinish(int totalItems, ItemToProcess[] startvalues) { bool allDone = false; while (!allDone) { allDone = true; for (int j = 0; j < totalItems; j++) { if (!startvalues[j].hasProcessed) { allDone = false; break; } } } }
private void ProcessItemsThreaded(ItemToProcess[] items) { int maxThreads, minThreads, completionPortThreads, completionPortThreads2; ThreadPool.GetMaxThreads(out maxThreads, out completionPortThreads); ThreadPool.GetMinThreads(out minThreads, out completionPortThreads2); Trace.WriteLine("maxThreads = " + maxThreads + ", minThreads = " + minThreads + "compThreads = " + completionPortThreads + ", " + completionPortThreads2); ThreadPool.SetMinThreads(100, completionPortThreads2); for (int i = 0; i < items.Length; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(callbackMethod), new SimpleTaskThread(items[i])); } }
public SimpleTaskThread(ItemToProcess item) { this.item = item; id = item.startValue; }
private void ProcessItemsNonThreaded(ItemToProcess[] items) { for (int i = 0; i < items.Length; i++) { SimpleTaskThread simple = new SimpleTaskThread(items[i]); simple.DoSomehing(); } }