// constructor method for the class that saves the constructor parameters public WorkerThread(WorkerInstance workerInstance, WorkerThreadFormatter workerThreadFormatter) { // save the constructor references this.workerInstance = workerInstance; threadDefinition = workerThreadFormatter; threadName = "Thread " + threadDefinition.ThreadNumber.ToString(); // define the perfmon counter instance descriptions perfCounterServiceName = MSMQListner.PerformanceCounterTotal; perfCounterWorkerName = threadDefinition.ProcessName; perfCounterThreadName = threadDefinition.ProcessName + "_" + threadDefinition.ThreadNumber.ToString(); // indicate the thread is not running workerRunning = false; workerPaused = false; }
// the work object start method that creates all the thread objects // each thread object then has it corresponding start method called internal void StartService() { // ensure the start method has not already been called if (!workerRunning) { // flag the worker as started workerRunning = true; workerDefinition.ProcessStatus = WorkerFormatter.SFProcessStatus.StatusStarted; // attempt to allocate all needed resources try { // call the event logging initilization class CreateLogClass(); // indicate the worker process has started LogInformation("Process Starting - Queue " + workerDefinition.InputQueue); // create the array list for the thread objects // insert into the array list the required number of thread objects threadReferences = new ArrayList(); // for each worker create and start the appropriate worker threads for (int idx = 0; idx < workerDefinition.NumberThreads; idx++) { WorkerThreadFormatter threadDefinition = new WorkerThreadFormatter(); threadDefinition.ProcessName = workerDefinition.ProcessName; threadDefinition.ProcessDesc = workerDefinition.ProcessDesc; threadDefinition.ThreadNumber = idx; threadDefinition.InputQueue = workerDefinition.InputQueue; threadDefinition.ErrorQueue = workerDefinition.ErrorQueue; threadDefinition.SmtpServer = workerDefinition.SmtpServer; threadDefinition.ErrorEmails = workerDefinition.ErrorEmails; threadDefinition.Subject = workerDefinition.Subject; threadDefinition.MessageLimit = workerDefinition.MessageLimit; threadDefinition.Delay = workerDefinition.Delay; // define the worker type and insert into the work thread struct WorkerThread workerThread = new WorkerThreadDerived(this, threadDefinition); threadReferences.Insert(idx, workerThread); } // call the start method on each thread object foreach (WorkerThread threadReference in threadReferences) { threadReference.StartService(); } // indicate the worker process has started LogInformation("Process Started"); } // on error call the stop method to tidyup created objects catch (Exception ex) { StopService(); LogError("Unable to Start Process"); throw new ApplicationException(ex.Message); } } }
// calls the base class constructor public WorkerThreadDerived(WorkerInstance workerInstance, WorkerThreadFormatter workerThreadFormatter) : base(workerInstance, workerThreadFormatter) { }