Files
Chris Kaczor 2e14ff032b
All checks were successful
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Successful in 1m12s
Adjust logging and add debugger option
2026-01-27 19:59:51 -05:00

119 lines
4.3 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.Information("Start");
if (args.Contains("--install", StringComparer.InvariantCultureIgnoreCase))
{
Log.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.Error(exception, "");
}
Log.Information("Install complete");
}
else if (args.Contains("--uninstall", StringComparer.InvariantCultureIgnoreCase))
{
Log.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.Error(exception, "");
}
Log.Information("Uninstall complete");
}
else
{
Log.Information("Starting");
try
{
while (true)
{
Log.Information("Creating PipeSecurity");
var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));
Log.Information("Creating NamedPipe");
var pipeWithSecurity = NamedPipeServerStreamAcl.Create(HardwareMonitorService.PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 0, 0, pipeSecurity);
Log.Information("Creating PipeServer");
var pipeServer = new PipeServer<IHardwareMonitorService>(new HardwarePipeSerializer(), pipeWithSecurity, () => new HardwareMonitorService());
Log.Information("Waiting for connection");
await pipeServer.WaitForConnectionAsync().ConfigureAwait(false);
Log.Information("Waiting for remote pipe to close");
await pipeServer.WaitForRemotePipeCloseAsync().ConfigureAwait(false);
Log.Information("Disposing pipe server");
pipeServer.Dispose();
}
}
catch (Exception exception)
{
Log.Error(exception, "");
}
}
Log.Information("Closing");
}
}