public static void Main(string[] args) { // configure the auto mappings AutoMapperConfig.CreateMappings(); Console.Title = ConfigurationManager.AppSettings["AppName"].ToString(); var repositoy = new SystemJobRepository(); var keepRunning = true; var jobWorkers = new List <JobWorker>(); while (keepRunning) { Console.WriteLine("Reading system jobs from database..."); var systemJobs = repositoy.GetAllIncompleteJobs(); // iterate and execute the jobs foreach (var job in systemJobs) { // check if the task is cancelled if (job.Status == JobStatus.Canceled) { var cancelWorker = jobWorkers.FirstOrDefault(x => x.JobId == job.Id); if (cancelWorker != null) { cancelWorker.CancelJob(); // remove it from the list jobWorkers.Remove(cancelWorker); } continue; } // don't add if already in the list var isAlreadyAdded = jobWorkers.Exists(x => x.JobId == job.Id); if (isAlreadyAdded) { continue; } // create and add the job worker created var jobWorker = createJobWorker(job); jobWorkers.Add(jobWorker); // start the job jobWorker.StartJob(); } // let's sleep the main thread for a while goToSleep(); // remove the jobs that are done from the list jobWorkers.RemoveAll(x => x.IsJobDone); } Console.ReadKey(); }
public JobWorker(SystemJob job) { _systemJob = job; _logger = new LoggerRepository(); _jobRepository = new SystemJobRepository(); _resultFileDirecctory = ConfigurationManager.AppSettings["SystemJobsResultFileRoot"].ToString(); // init the Background Worker _bw = new BackgroundWorker(); _bw.DoWork += new DoWorkEventHandler(bw_DoWork); _bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); _bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); _bw.WorkerReportsProgress = true; _bw.WorkerSupportsCancellation = true; }