我正在尝试创建Windows服务,该服务将管理启动多个exe
/ bat
文件。
到目前为止,我已经遵循此guide并能够使用以下代码启动exe。但是,当我停止该服务时,生成的exe似乎已分离,并且我不确定如何以编程方式将其杀死。
最终,我想启动和停止许多不同的bat
和exe
。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Runtime.InteropServices;
namespace SomeService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Process.Start("C:\\Users\\JohnnySmalls\\SomeProgram\\bin\\theExe.exe");
}
protected override void OnStop()
{
// Need to close process, I don't have a reference to it though
// Process.Close();
}
}
}
答案 0 :(得分:0)
您可以这样做
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Runtime.InteropServices;
namespace SomeService
{
public partial class Service1 : ServiceBase
{
private List<Process> _processes;
public Service1()
{
InitializeComponent();
_processes = new List<Process>();
}
protected override void OnStart(string[] args)
{
var process = Process.Start("C:\\Users\\JohnnySmalls\\SomeProgram\\bin\\theExe.exe");
_processes.Add(process);
}
protected override void OnStop()
{
// Need to close process, I don't have a reference to it though
// Process.Close();
foreach(var process in _processes) {
process.Close();
}
}
}
}