public void start() { BaseWorker th = new BaseWorker(runThreadsLoop); th.Name = "Main Thread Loop"; th.createThread(ref th); th.Start(); }
/// <summary> /// This function will take in a worker instance object, a callback handle, and /// a social media type. Then it will create and queue a thread for running. /// </summary> /// <param name="worker">The worker instance object.</param> public void createQueueThread(BaseWorker worker) { //create worker thread m_TotalThreads++; worker.queueCallback += new BaseWorker.queueCallbackDelegate(queueCallback); worker.threadCallback += new BaseWorker.threadCallbackDelegate(threadCallback); worker.progressCallback += new BaseWorker.progressCallbackDelegate(progressCallback); worker.m_SeedThread = true; worker.createThread(ref worker); lock (m_Lock) { //queue thread m_ThreadQueue.Add(worker); } }
/// <summary> /// This function serves as the master run thread loop. /// </summary> public void runThreadsLoop() { DateTime printStatusTime = DateTime.Now; int count = 0; BaseWorker cancelTh = new BaseWorker(this.setCancelledThreads); cancelTh.Name = "setCancelledThreads"; cancelTh.createThread(ref cancelTh); cancelTh.Start(); BaseWorker activeTh = new BaseWorker(this.setActiveThreads); activeTh.Name = "setActiveThreads"; activeTh.createThread(ref activeTh); activeTh.Start(); BaseWorker maxTh = new BaseWorker(this.setMaxThreads); maxTh.Name = "setMaxThreads"; maxTh.createThread(ref maxTh); maxTh.Start(); BaseWorker gcTh = new BaseWorker(this.gcCleanup); gcTh.Name = "gcCleanup"; gcTh.createThread(ref gcTh); gcTh.Start(); // maybe create 3 threads to manage the arrays. while (m_Run) { DateTime start = DateTime.Now; int completedThreads = 0; if (DateTime.Now - printStatusTime > TimeSpan.FromSeconds(5) && (m_DeadThreads.Count > 0 || m_ActiveThreads.Count > 0)) { start = DateTime.Now; for (int i = 0; i < m_DeadThreads.Count && DateTime.Now - start < TimeSpan.FromSeconds(2); i++) { if (m_DeadThreads[i] == null || m_DeadThreads[i].Join(150)) { //m_DeadThreads[i] = null; m_DeadThreads.RemoveAt(i); //Console.Write("."); i--; completedThreads++; } } #if DEBUG Console.WriteLine(m_InstanceName + "\n" + "Added Threads: " + m_TotalThreads + "\nQueued Threads: " + m_ThreadQueue.Count + "\nActive Threads: " + m_ActiveThreads.Count + "\nCompleted Threads: " + completedThreads + "\nDead Threads: " + m_DeadThreads.Count + "\nMax Threads: " + m_MaxThreads + "\nRequested Threads: " + MaxThreads); m_TotalThreads = 0; // added threads completedThreads = 0; #endif printStatusTime = DateTime.Now; } else { if (m_ActiveThreads.Count > m_MaxThreads) { Thread.Sleep(120); } //else // Thread.Sleep(300); } Thread.Sleep(120); } //update history to complete Console.WriteLine("All threads complete, press any key to continue."); }