Files
HardwareMonitorStatusWindow/Service/Program.cs
Chris Kaczor 13ae4c74bc
All checks were successful
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Successful in 52s
Add separate logs
2026-01-27 19:23:11 -05:00

111 lines
4.2 KiB
C#

using System.IO.Pipes;
using System.Security.AccessControl;
using System.Security.Principal;
using Microsoft.Win32.TaskScheduler;
using PipeMethodCalls;
using Serilog;
using Task = System.Threading.Tasks.Task;
namespace HardwareMonitorStatusWindow.Service;
internal class Program
{
private static async Task Main(string[] args)
{
Log.Logger = new LoggerConfiguration().WriteTo.File("HardwareMonitorService.log").CreateLogger();
Log.Logger.Information("Start");
if (args.Contains("--install", StringComparer.InvariantCultureIgnoreCase))
{
Log.Logger.Information("Starting install...");
try
{
using var taskService = new TaskService();
var existingTask = taskService.FindTask(HardwareMonitorService.ScheduledTaskName);
if (existingTask == null)
{
var taskDefinition = taskService.NewTask();
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
taskDefinition.Triggers.Add(new LogonTrigger { Delay = TimeSpan.FromSeconds(30) });
taskDefinition.Actions.Add(new ExecAction(Environment.ProcessPath!));
taskDefinition.Settings.RestartInterval = TimeSpan.FromMinutes(1);
taskDefinition.Settings.RestartCount = 3;
taskDefinition.Settings.StartWhenAvailable = true;
taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.Zero;
taskDefinition.Settings.StopIfGoingOnBatteries = false;
taskDefinition.Settings.DisallowStartIfOnBatteries = false;
taskService.RootFolder.RegisterTaskDefinition(HardwareMonitorService.ScheduledTaskName, taskDefinition);
}
existingTask = taskService.FindTask(HardwareMonitorService.ScheduledTaskName);
existingTask.Run();
}
catch (Exception exception)
{
Log.Logger.Error(exception, "Install");
}
Log.Logger.Information("Install complete");
}
else if (args.Contains("--uninstall", StringComparer.InvariantCultureIgnoreCase))
{
Log.Logger.Information("Starting uninstall...");
try
{
using var taskService = new TaskService();
var existingTask = taskService.FindTask(HardwareMonitorService.ScheduledTaskName);
existingTask?.Stop();
taskService.RootFolder.DeleteTask(HardwareMonitorService.ScheduledTaskName, false);
}
catch (Exception exception)
{
Log.Logger.Error(exception, "Uninstall");
}
Log.Logger.Information("Uninstall complete");
}
else
{
Log.Logger.Information("Starting");
try
{
while (true)
{
var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));
var pipeWithSecurity = NamedPipeServerStreamAcl.Create(HardwareMonitorService.PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 0, 0, pipeSecurity);
var pipeServer = new PipeServer<IHardwareMonitorService>(new HardwarePipeSerializer(), pipeWithSecurity, () => new HardwareMonitorService());
//var pipeServer = new PipeServer<IHardwareMonitorService>(
// new HardwarePipeSerializer(),
// HardwareMonitorService.PipeName,
// () => new HardwareMonitorService());
await pipeServer.WaitForConnectionAsync().ConfigureAwait(false);
await pipeServer.WaitForRemotePipeCloseAsync().ConfigureAwait(false);
pipeServer.Dispose();
}
}
catch (Exception exception)
{
Log.Logger.Error(exception, "");
}
}
Log.Logger.Information("Closing");
}
}