C# 使用windows服务连续运行.exe文件
我想运行一个exe文件,如果此exe关闭,windows服务必须通过按时间间隔检查是否关闭来重新启动此exeC# 使用windows服务连续运行.exe文件,c#,windows-services,C#,Windows Services,我想运行一个exe文件,如果此exe关闭,windows服务必须通过按时间间隔检查是否关闭来重新启动此exe static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main() { ServiceBase[] ServicesToRun;
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
string filename = "C:\\a.exe";
Process.Start(filename);
ServiceBase.Run(ServicesToRun);
}
}
静态类程序
{
///
///应用程序的主要入口点。
///
静态void Main()
{
ServiceBase[]ServicesToRun;
ServicesToRun=新的ServiceBase[]
{
新服务1()
};
string filename=“C:\\a.exe”;
Process.Start(文件名);
ServiceBase.Run(ServicesToRun);
}
}
我想这就是你要找的
public partial class App : System.Windows.Application
{
public bool IsProcessOpen(string name)
{
foreach (Process clsProcess in Process.GetProcesses()) {
if (clsProcess.ProcessName.Contains(name))
{
return true;
}
}
return false;
}
protected override void OnStartup(StartupEventArgs e)
{
// Get Reference to the current Process
Process thisProc = Process.GetCurrentProcess();
if (IsProcessOpen("name of application.exe") == false)
{
//System.Windows.MessageBox.Show("Application not open!");
//System.Windows.Application.Current.Shutdown();
}
else
{
// Check how many total processes have the same name as the current one
if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)
{
// If ther is more than one, than it is already running.
System.Windows.MessageBox.Show("Application is already running.");
System.Windows.Application.Current.Shutdown();
return;
}
base.OnStartup(e);
}
}
你的问题是什么?