private void StartProcess(UserDatabase userDatabase) { var threadName = $"ThreadDbId{userDatabase.DatabaseId}"; var count = _databaseManager.ExecuteScalar(userDatabase.ConnectionString, "sc_calculation_queue_count"); Common.Repository.IPricingRepository repo = new Common.Repository.PricingRepository(userDatabase.ConnectionString, Constants.TIMEOUT_MSMQ_EXEC_STOREDPROC); Common.Engine.IPriceEngine engine = new Common.Engine.PriceEngine(repo, Logger); if (count > this._appConfiguration.NewThreadMessages) { if (ActiveThreads.Count <= _appConfiguration.MaxThreadCount) { if (this.ActiveThreads.All(t => t.Name != threadName)) { var t = new Thread(() => Process(engine, userDatabase, count)) { Name = threadName }; t.Start(); ActiveThreads.Add(t); } } } else { Process(engine, userDatabase, count); } }
/// <summary> /// Creates and starts a thread for our thread manager to keep track of. /// </summary> /// <param name="functionToRun">The function the thread should run.</param> /// <returns>The created thread</returns> public static CustomThread CreateThread(ThreadStart functionToRun) { CustomThread thread = new CustomThread(functionToRun); ActiveThreads.Add(thread); return(thread); }
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; } }