protected override void OnDoWork(DoWorkEventArgs e) { if (!Started) { Started = true; workerThread = new CThread() { Worker = Thread.CurrentThread }; Log.Write("[Threading] Starting new thread (" + workerThread.Id + ") of type: '" + workerThread.Type + "' with scheduler id: " + workerThread.Worker.ManagedThreadId, 7); try { base.OnDoWork(e); } catch (ThreadAbortException) { e.Cancel = true; Thread.ResetAbort(); } if (workerThread != null) { Log.Write("[Threading] Thread id: " + workerThread.Worker.ManagedThreadId + " (" + workerThread.Id + ") has completed.", 7); } } }
public void Abort() { if (workerThread != null) { workerThread.Abort(); workerThread = null; } }
// zzz - need to improve http://stackoverflow.com/questions/8028483/managing-threads-using-listthread public CThreadManager() { // start session manager thread Thread.CurrentThread.Name = CFunctions.GenerateUID(); ThreadManager = new CThread() { Worker = new Thread(new ThreadStart(ManageThreads)) }; Log.Write("[Threading] Primary thread (" + Thread.CurrentThread.Name + ") of type: '" + ThreadManager.Type + "' with scheduler id: " + Thread.CurrentThread.ManagedThreadId + " started at: " + Core._StartTime, 7); ThreadManager.Start(); Log.Write("[Threading] Starting new thread (" + ThreadManager.Id + ") of type: '" + ThreadManager.Type + "' with scheduler id: " + ThreadManager.Worker.ManagedThreadId, 7); }
public void ManageThreads() { do { int tc = 0; for (int i = 0; i < ActiveThreads.Count; i++) { CThread thread = ActiveThreads[i]; if (thread.Type != CThread.Types.Unmanaged) { tc++; } if (thread.Completed) { CompletionCount++; Log.Write("[Threading] Thread id: " + thread.Worker.ManagedThreadId + " (" + thread.Id + ") has completed.", 7); ActiveThreads.Remove(thread); } } for (int i = 0; i < QueuedThreads.Count; i++) { if (tc < COptions.Thread_Max_Total) { bool start = false; CThread thread = QueuedThreads[i]; if ((thread.Type == CThread.Types.VMware) && (thread.Session.ThreadCount < COptions.Thread_Max_VI_Per_Instance)) { start = true; thread.Session.ThreadCount++; } else if ((thread.Type == CThread.Types.HyperV) && (thread.Session.ThreadCount < COptions.Thread_Max_HV_Per_Instance)) { start = true; thread.Session.ThreadCount++; } else if ((thread.Type == CThread.Types.Unmanaged)) { start = true; } if (start) { Log.Write("[Threading] Starting new thread (" + thread.Id + ") of type: '" + thread.Type + "' with scheduler id: " + thread.Worker.ManagedThreadId, 7); thread.Start(); ActiveThreads.Add(thread); QueuedThreads.Remove(thread); } } } Thread.Sleep(COptions.Thread_Wait); } while (!Terminated); Log.Write("[Threading] Thread id: " + Thread.CurrentThread.ManagedThreadId + " (" + ThreadManager.Id + ") has completed.", 7); }
public void Add(CThread thread) { thread.Worker.Name = thread.Id; QueuedThreads.Add(thread); if (QueuedCount == -1) { QueuedCount = 1; } else { QueuedCount++; } }