无法重新启动服务 [英] Cannot restart a Service

查看:146
本文介绍了无法重新启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码来重新启动服务,但是这是行不通的。



我就可以开始和独立停止,但不会重新启动它涉及我的第一站,并启动。服务

 
{
//service.Stop();
//service.Start();
INT millisec1 = Environment.TickCount;
超时时间跨度= TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped,超时);

//算超时
INT millisec2 = Environment.TickCount的休息;
超时= TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));

service.Start();
service.WaitForStatus(ServiceControllerStatus.Running,超时);
}

{
// ...
}

很简单,就是要在抓一节。



我不知道我要去的地方错了。



任何建议??



更新:



所以我把这个想法从下面的正确答案:



这是什么需要做>

 公共静态无效RestartService(字符串服务名,诠释timeoutMilliseconds)
{
ServiceController的服务=新的ServiceController(服务名);

INT millisec1 = Environment.TickCount;
超时时间跨度= TimeSpan.FromMilliseconds(timeoutMilliseconds);
如果(!(service.Status.Equals(ServiceControllerStatus.Stopped)|| service.Status.Equals(ServiceControllerStatus.StopPending)))
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped,超时);
}
//算超时$ B $其余b INT millisec2 = Environment.TickCount;
超时= TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));

如果(!(service.Status.Equals(ServiceControllerStatus.Running)|| service.Status.Equals(ServiceControllerStatus.StartPending)))
{
service.Start() ;
service.WaitForStatus(ServiceControllerStatus.Running,超时);
}
}


解决方案

有一个空的catch块捕获所有异常很少是一个好主意,因为严重的问题,很容易滑过。



修改代码以至少做一些记录在catch块,如

 赶上(异常前)
{
System.Diagnostics.Trace.WriteLine(ex.ToString() );
}



然后,您可以附加一个跟踪侦听器在服务或使用的构造Sysinternals公司的 DebugView中工具读取跟踪消息。如果你想进一步采取这个步骤中,您可能要包括日志库像log4net的到您的项目。



我的猜测是,你得到一个 TimeoutException异常因为停止该服务需要为你预期的要长。 ?您是否尝试过增加超时或删除超时参数无限等待



更新:



您可能需要检查你的服务是否启动与否:

  IF((service.Status.Equals (ServiceControllerStatus.Stopped)
|| service.Status.Equals(ServiceControllerStatus.StopPending)))
{
service.Stop();
}
service.Start();


I have this code to restart a service, but this is not working.

I can start and stop individually but not restart which involves me to first stop and start the service.

try
{
    //service.Stop();
    //service.Start();
    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));

    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
    // ...
}

It is simply going in the catch section.

I don't know where i am going wrong.

Any suggestions.??

UPDATE:

So I took the idea from the correct answer below:

This is what need to be done>

public static void RestartService(string serviceName, int timeoutMilliseconds)
{
    ServiceController service = new ServiceController(serviceName);

    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
    if (!(service.Status.Equals(ServiceControllerStatus.Stopped) || service.Status.Equals(ServiceControllerStatus.StopPending)))
    {
        service.Stop();
        service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    }
    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));

    if (!(service.Status.Equals(ServiceControllerStatus.Running) || service.Status.Equals(ServiceControllerStatus.StartPending)))
    {
        service.Start();
        service.WaitForStatus(ServiceControllerStatus.Running, timeout);
    }
}

解决方案

Having an empty catch block catching all exceptions is rarely a good idea as serious problems easily slip through.

Modify your code to at least do some logging in the catch block, e.g.

catch (Exception ex)
{
    System.Diagnostics.Trace.WriteLine(ex.ToString());
}

You can then either attach a trace listener in the constructor of your service or use the DebugView tool from Sysinternals to read the trace message. If you want to take this a step further you might want to include a logging library like log4net into your project.

My guess would be that your are getting a TimeoutException because stopping the service takes longer as you expected. Have you tried increasing the timeout or waiting infinitely by removing the timeout parameter?

Update:

You probably need to check whether your service is started or not:

if  (!(service.Status.Equals(ServiceControllerStatus.Stopped) 
       || service.Status.Equals(ServiceControllerStatus.StopPending)))
{
    service.Stop();
}
service.Start();

这篇关于无法重新启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆