kmCustomReportsNET/ClassObj/ServiceMain.cs

71 lines
1.5 KiB
C#
Raw Normal View History

2025-10-08 02:16:58 -04:00
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<Task> tasks = new();
2025-11-11 04:24:46 -05:00
Version? AssemblyVersion = null;
2025-10-08 02:16:58 -04:00
public bool Start(HostControl hostControl)
{
2025-11-11 04:24:46 -05:00
try
{
AssemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
}
catch
{
AssemblyVersion = new Version("1.0");
}
2025-10-08 02:16:58 -04:00
tasks.Add(Task.Run(() => { Looper(); }));
2025-11-11 04:24:46 -05:00
appLog.Info(string.Format("Service successfully started - v{0}", AssemblyVersion.ToString()));
2025-10-08 02:16:58 -04:00
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));
2025-10-20 09:22:53 -04:00
if (ClsConstants.CanWeGo(1) && dtNext < DateTimeOffset.Now)
2025-10-08 02:16:58 -04:00
{
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();
}
}
}