public static void Add(ParameterizedThreadStart parameterizedThreadStart, Object Parameter) { ThreadValuePair tvPair = new ThreadValuePair(); tvPair.Thread = new Thread(parameterizedThreadStart); tvPair.Parameter = Parameter; if (ActiveThreads.Count() < MaxActiveThreads) { ActiveThreads.Add(tvPair.Thread); tvPair.Thread.Start(tvPair.Parameter); } else { QueuedThreads.Enqueue(tvPair); } }
public static void Add(ThreadStart threadStart) { ThreadValuePair tvPair = new ThreadValuePair(); tvPair.Thread = new Thread(threadStart); tvPair.Parameter = null; if (ActiveThreads.Count() < MaxActiveThreads) { ActiveThreads.Add(tvPair.Thread); tvPair.Thread.Start(); } else { QueuedThreads.Enqueue(tvPair); } }
private static void timer_callback(object state) { if (!timerCallbackExecuting) { timerCallbackExecuting = true; //Monitor.Enter(ActiveThreads); //Monitor.Enter(QueuedThreads); //foreach (Thread thread in ActiveThreads) for (int index = ActiveThreads.Count() - 1; index >= 0; index--) { //Thread thread = ActiveThreads[index]; if (!ActiveThreads[index].IsAlive) { ActiveThreads.Remove(ActiveThreads[index]); } //else if (ActiveThreads[index].ThreadState == ThreadState.Aborted || ActiveThreads[index].ThreadState == ThreadState.Stopped) // ActiveThreads.Remove(ActiveThreads[index]); } while (ActiveThreads.Count() < MaxActiveThreads && QueuedThreads.Count() > 0) { ThreadValuePair threadDataPair = QueuedThreads.Dequeue(); Thread thread = threadDataPair.Thread; ActiveThreads.Add(thread); if (ReferenceEquals(threadDataPair.Parameter, null)) { thread.Start(); } else { thread.Start(threadDataPair.Parameter); } } //Monitor.Exit(ActiveThreads); //Monitor.Exit(QueuedThreads); timerCallbackExecuting = false; } }