67 lines
2.5 KiB
C#
67 lines
2.5 KiB
C#
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using OI.Metrology.Console.Models;
|
|
using System.Diagnostics;
|
|
|
|
namespace OI.Metrology.Console;
|
|
|
|
public class Worker : BackgroundService
|
|
{
|
|
|
|
private readonly List<string> _Args;
|
|
private readonly ILogger<Worker> _Logger;
|
|
private readonly AppSettings _AppSettings;
|
|
private readonly IHostApplicationLifetime _Lifetime;
|
|
|
|
public Worker(ILogger<Worker> logger, IHostApplicationLifetime lifetime, List<string> args, AppSettings appSettings)
|
|
{
|
|
_Args = args;
|
|
_Logger = logger;
|
|
_Lifetime = lifetime;
|
|
_AppSettings = appSettings;
|
|
}
|
|
|
|
public override Task StartAsync(CancellationToken cancellationToken) =>
|
|
base.StartAsync(cancellationToken);
|
|
|
|
public override Task StopAsync(CancellationToken cancellationToken) =>
|
|
base.StopAsync(cancellationToken);
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
if (!stoppingToken.IsCancellationRequested)
|
|
await Task.Delay(500, stoppingToken);
|
|
if (_AppSettings is null)
|
|
throw new NullReferenceException(nameof(_AppSettings));
|
|
try
|
|
{
|
|
_Logger.LogInformation("BaseDirectory: <{BaseDirectory}>", AppContext.BaseDirectory);
|
|
_Logger.LogInformation("CurrentDirectory: <{CurrentDirectory}>", Environment.CurrentDirectory);
|
|
_Logger.LogInformation("Press Escape key to get -1, Enter key to get 0, y key to get 1 and n key to get 2 exit code");
|
|
ConsoleKeyInfo consoleKeyInfo = System.Console.ReadKey();
|
|
if (consoleKeyInfo.Key == ConsoleKey.W)
|
|
{
|
|
Process process = Process.Start("L:/DevOps/Mesa_FI/OI-Metrology/WinForms/bin/Debug/net8.0-windows/win-x64/OI.Metrology.WinForms.exe", "Mike");
|
|
process.WaitForExit();
|
|
_Logger.LogInformation("ExitCode: <{ExitCode}>", process.ExitCode);
|
|
}
|
|
else
|
|
{
|
|
Environment.ExitCode = consoleKeyInfo.Key switch
|
|
{
|
|
ConsoleKey.Escape => -1,
|
|
ConsoleKey.Enter => 0,
|
|
ConsoleKey.Y => 1,
|
|
ConsoleKey.N => 2,
|
|
_ => 9
|
|
};
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{ _Logger.LogError("{Message}{NewLine}{StackTrace}", ex.Message, Environment.NewLine, ex.StackTrace); }
|
|
_Logger.LogInformation("Done. Press 'Enter' to end");
|
|
_ = System.Console.ReadLine();
|
|
_Lifetime.StopApplication();
|
|
}
|
|
|
|
} |