Initial WIP commit
Some checks failed
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Failing after 9s
Some checks failed
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Failing after 9s
This commit is contained in:
111
Service/Program.cs
Normal file
111
Service/Program.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
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("log.txt").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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user