using log4net; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Topshelf; namespace kmCustomReportsNET.ClassObj; internal class ServiceMain : ServiceControl { ILog appLog = LogManager.GetLogger(Program.AppName); ConcurrentBag tasks = new(); public bool Start(HostControl hostControl) { tasks.Add(Task.Run(() => { Looper(); })); appLog.Info(string.Format("Service successfully started.")); return true; } //Start public bool Stop(HostControl hostControl) { Program.isExiting = true; Task.WaitAll(tasks.ToArray()); return true; } //Stop private void Looper() { var dtNext = DateTimeOffset.Now.AddSeconds(15); var rn = new Random(); while (!Program.isExiting) { Thread.Sleep(rn.Next(250, 1500)); if (dtNext < DateTimeOffset.Now) { try { RunTheReports(); } catch //(Exception) { // do nothing } dtNext = DateTimeOffset.Now.AddSeconds(rn.Next(60, 90)); } } } private void RunTheReports() { using (var obj = new ClsDobbsEmail_T1439() { DebugMode = false }) { obj.Go(); } } }