2015-10-19 3 views
-1

Я хочу запустить exe-файл, и если это exe-shutdown, служба Windows должна снова запустить этот exe, проверив, закрыт или нет с временным интервалом.Запуск файла .exe непрерывно с помощью службы Windows

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); 
    } 
} 
+0

Что ваш вопрос? –

ответ

0

Я думаю, что это то, что вы ищете

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); 
    } 
} 
Смежные вопросы