private void Loop() { #if DEBUG Thread.Sleep(TimeSpan.FromMinutes(1)); #endif int count = 0; while (running) { try { var logic = new JobManagement.JobsCore(ConfigurationManager.AppSettings["database"]); var jobs = logic.GetJobs(); // Process up to 100 items in one cycle jobs = jobs.GetRange(0, Math.Min(jobs.Count, 100)); foreach (var job in jobs) { string type; IDictionary <string, string> parameters; logic.GetJobDetails(job, out type, out parameters); ProcessJob(type, parameters); count++; // Mark this job as complete asynchronously logic.FinishJob(job); if (!running) { break; } } if (jobs.Count == 0) { Thread.Sleep(500); } // Delete old jobs periodically if (count > 100) { count = 0; // Delete olbs jobs asynchronously logic.DeleteOldJobs(); } } catch (Exception ex) { System.Diagnostics.EventLog.WriteEntry("JobProcessor", ex.ToString()); } } }
public List <string> GetJobs() { var jobs = new JobManagement.JobsCore(ConfigurationManager.AppSettings["database"]); // Run the logic in a separate thread asynchronously, kind of example of the bad code pattern var t = Task.Run(() => jobs.GetJobs()); return(t.Result); }