2013-09-23 243 views
0

我想知道如何以我想要的方式重新启动我的Windows服务。Windows服务重新启动

我有这样的解决方案:

private const int RestartTimeout = 60000; // 1 minute 

public void Control(string serviceName) 
{ 
    service = new ServiceController(serviceName); 
} 

public bool RestartService() 
     { 
      try 
      { 
       Control("MyService"); 
       service.Refresh(); 
       if (service.Status != ServiceControllerStatus.Stopped) 
       { 
        mytimer.Enabled = false; 
        service.Stop(); 
        int i = 0; 
        service.Refresh(); 

        while (service.Status == ServiceControllerStatus.Stopped || service.Status == ServiceControllerStatus.StopPending) 
        { 
         i++; 
         Thread.Sleep(100); 
         if (i >= RestartTimeout/100) 
         { 
          return false; 
         } 
        } 
        service.Start(); 
        return true; 
       } 

       service.Start(); 

       return true; 
      } 
      catch (Exception ex) 
      { 
       return false; 
      } 

     } 

我得到超时错误,当它到达while循环。我想这是因为服务已经停止了?

我真正想在这里实现的是停止服务,然后再次启动服务之前再睡1分钟。

Please help.Thanks!谢谢!

+0

出于兴趣,重新启动服务有什么用? –

+0

我的服务通常只运行3-5分钟,但如果超过这个时间,它将自动重新启动,以防止它挂起或吃掉我的机器上的资源。谢谢 – SyntaxError

+0

当它没有做任何事情时它不能睡觉吗? –

回答

0

我解决了它这种方式(当然,你需要开始用管理员权限您的申请!):

小编建议:分割你的问题分成多个部分(多种方法),因为如果出现错误,你可以缩小它下降到少量的线路。

public void RestartService(string serviceName) 
    { 
     if (StopService(serviceName) == true) 
     { 
      Thread.Sleep(60000); 
      if (StartService(serviceName) == true) 
      { 
       MessageBox.Show("Service started succesfully"); 
      } 
     } 
    } 

    private bool StartService(string serviceName) 
    { 
     ServiceController service = new ServiceController(serviceName); 
     try 
     { 
      service.Start(); 
      service.WaitForStatus(ServiceControllerStatus.Running); 
      return true; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
      return false; 
     } 
    } 

    private bool StopService(string serviceName) 
    { 
     ServiceController service = new ServiceController(serviceName); 
     try 
     { 
      if (service.Status != ServiceControllerStatus.Stopped) 
      { 
       service.Stop(); 
       service.WaitForStatus(ServiceControllerStatus.Stopped); 
       return true; 
      } 
      else if (service.Status == ServiceControllerStatus.Stopped) 
      { 
       return true; 
      } 
      return false; 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
      return false; 
     } 
    } 
+0

谢谢。这个工作,但只有当我设置线程睡眠20000-25000。更重要的是,它不会再进入服务的启动。我有10秒初始化时间。这是超时问题吗?35秒超时? – SyntaxError

+0

通常服务在30秒后超时(这是默认值)。您可以通过更改注册表中的一个键来延长这段时间: 'HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control' - > 'ServicesPipeTimeout'。将值从'30000'改为'100000' –

0

我有一个类似的asp.net项目,我用这个代码,这是工作。

try 
{ 
ServiceController service = new ServiceController(service_name, computer_name); 
if (service.Status != ServiceControllerStatus.Running) 
{ 
return false; 
} 
else 
{ 
service.Stop(); 
Thread.Sleep(60000); 
service.Start(); 
} 
} 
catch (Exception exc) 
{ 
return false; 
} 

我知道你可以做到这一点,但有时它更好只是想想简单。

+0

当它到达线程时,我仍然有一个超时错误。睡眠(60000)。::( – SyntaxError