private void CheckForRun() { if (isProcessing == false && nextRun <= DateTime.Now) { EventLogger.DebugEntry("Creating complex thread", EventLogEntryType.Information); ThreadHolder exportThread = new ThreadHolder(); Guid guid = Guid.NewGuid(); exportThread.scanThread = new ScanThread(this, guid); // Here you can add the resuming, on certain conditions // In my case I insert data to myThread which contains all the necessary objects to continue processing from the last halt // exportThread.myThread = new MyThreadComplex(this, guid, threadType, 3); if (CreateWorkerThread(exportThread, guid)) { isProcessing = true; EventLogger.DebugEntry("Creating complex thread - successful", EventLogEntryType.Information); } else { EventLogger.Entry("Creating complex thread - failed", EventLogEntryType.Error); } } }
public int Run(T subjectInstance) { var allThreads = new List <ThreadHolder <T> >(); var launcher = new Thread( () => { try { for (var i = 0; i < _numThreads; i++) { var threadHolder = new ThreadHolder <T> { Thread = new Thread(ThreadProc) { Name = i.ToString() }, Subject = subjectInstance }; threadHolder.Thread.Start(threadHolder); allThreads.Add(threadHolder); if (i > 2 && TimeoutBetweenThreadStart > 0) { Thread.Sleep(TimeoutBetweenThreadStart); } } } catch (Exception e) { _errors.Enqueue(e); throw; } }); var totalLoops = 0; _running = true; // Use a separated thread for launching in case too many threads are asked: the inner Start will freeze // but would be able to resume once _running would have been set to false, causing first threads to stop. launcher.Start(); // Sleep for the required timeout, taking into account the start delay (if all threads are launchable without // having to wait due to thread starvation). Thread.Sleep(TimeoutBetweenThreadStart * _numThreads + EndTimeout); // Tell the threads to shut down, then wait until they all finish. _running = false; launcher.Join(); foreach (var threadHolder in allThreads.Where(t => t != null)) { threadHolder.Thread.Join(); totalLoops += threadHolder.LoopsDone; } return(totalLoops); }
private void AdjustThreadHolderCount() { while (_threadHolders.Count < _config.Threads) { ThreadHolder threadHolder = new ThreadHolder(_connection, _optionStatus); _threadHolders.Add(threadHolder); } while (_threadHolders.Count > _config.Threads) { ThreadHolder doomedThreadHolder = _threadHolders.Last(); doomedThreadHolder.Doom(_connection); _threadHolders.Remove(doomedThreadHolder); } }
public void DistributeOptions(List <ThreadHolder> threadHolders) { if (threadHolders.Any() == false) { return; } List <DatabaseOptionBase> databaseOptions = _databaseOptionFinder.Find(); foreach (DatabaseOptionBase option in databaseOptions) { ThreadHolder threadHolder = threadHolders.OrderBy(worker => worker.EstimatedOptionCount).FirstOrDefault(); option.AssignWorkerIfNoWorkerAssigned(threadHolder.DatabaseWorker, _connection); threadHolder.EstimatedOptionCount++; } }
private bool CreateWorkerThread(ThreadHolder exportThread, Guid guid) { using (LockHolder <Dictionary <Guid, ThreadHolder> > lockObj = new LockHolder <Dictionary <Guid, ThreadHolder> >(runningThreads, 1000)) { if (lockObj.LockSuccessful) { Thread thread = new Thread(exportThread.Process) { Name = "ScanThread" }; exportThread.thread = thread; runningThreads.Add(guid, exportThread); thread.Start(); return(true); } } return(false); }
private void CheckForRun() { if (isProcessing == false) { EventLogger.DebugEntry("Creating complex thread", EventLogEntryType.Information); ThreadHolder exportThread = new ThreadHolder(); Guid guid = Guid.NewGuid(); exportThread.scanThread = new ScanThread(this, guid); if (CreateWorkerThread(exportThread, guid)) { isProcessing = true; EventLogger.DebugEntry("Creating complex thread - successful", EventLogEntryType.Information); } else { EventLogger.Entry("Creating complex thread - failed", EventLogEntryType.Error); } } }
/// <summary> /// addThread function, adds a function as a thread to a list, which needs its own thread for running. This specific function handles ThreadStart parameter, which means it can only take functions with no parameters as argument /// </summary> /// <param name="_threadStart">Parameter is name of function that needs its own thread, as its Threadstart, it means it have to be a function with no parameters: (functionname(){})</param> /// <param name="_description">Description which the thread needs to be saved as (unique)</param> public void addThread(ThreadStart _threadStart, string _description) { ThreadHolder tempThreadHolder = find(_description); if(tempThreadHolder == null) { Thread tempThread = new Thread(_threadStart); ThreadHolder tempHolder = new ThreadHolder(); tempHolder.stringDescription = _description; tempHolder.threadPlaceHolder = tempThread; tempHolder.isWithParameter = false; tempHolder.objectFunctionHolder = _threadStart; threadList.Add(tempHolder); } if(tempThreadHolder != null) throw new ArgumentException("Thread already exists"); }
/// <summary> /// Remakes the thread so it can run under same name again. /// </summary> /// <param name="_threadHolder">Parameter is threadholder which holds what needs to be copied</param> private void remakeThread(ThreadHolder _threadHolder) { Thread threadTemporary; if (_threadHolder.isWithParameter) { threadTemporary = new Thread((ParameterizedThreadStart)_threadHolder.objectFunctionHolder); } else { threadTemporary = new Thread((ThreadStart)_threadHolder.objectFunctionHolder); } _threadHolder.threadPlaceHolder = threadTemporary; }