代码拉取完成,页面将自动刷新
using System.Diagnostics;
namespace RedisService
{
class Program
{
static void Main(string[] args)
{
string configFilePath = "redis.conf";
if (args.Length > 1 && args[0] == "-c")
{
configFilePath = args[1];
}
IHost host = Host.CreateDefaultBuilder().UseWindowsService().ConfigureServices((hostContext, services) =>
{
services.AddHostedService(serviceProvider =>
new RedisService(configFilePath));
}).Build();
host.Run();
}
}
public class RedisService(string configFilePath) : BackgroundService
{
private Process? redisProcess = new();
public override Task StartAsync(CancellationToken stoppingToken)
{
var basePath = Path.Combine(AppContext.BaseDirectory);
if (!Path.IsPathRooted(configFilePath))
{
configFilePath = Path.Combine(basePath, configFilePath);
}
configFilePath = Path.GetFullPath(configFilePath);
var diskSymbol = configFilePath[..configFilePath.IndexOf(":")];
var fileConf = configFilePath.Replace(diskSymbol + ":", "/cygdrive/" + diskSymbol).Replace("\\", "/");
string fileName = Path.Combine(basePath, "redis-server.exe").Replace("\\", "/");
string arguments = $"\"{fileConf}\"";
ProcessStartInfo processStartInfo = new(fileName, arguments)
{
WorkingDirectory = basePath
};
redisProcess = Process.Start(processStartInfo);
return Task.CompletedTask;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Delay(-1, stoppingToken);
}
public override Task StopAsync(CancellationToken stoppingToken)
{
if (redisProcess != null)
{
redisProcess.Kill();
redisProcess.Dispose();
}
return Task.CompletedTask;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。