0

I would like to know how I can restart my windows service the way I want it to be.

I have this solution:

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;
            }

        }

I got a timeout error when it reaches the while loop. I guess it's because the service has been stopped?

What I'm really trying to achieve here is to Stop the service and then sleep for 1 minute before starting the service again.

Please help.Thanks!

4

2 回答 2

1

我以这种方式解决了它(当然您需要以管理员权限启动您的应用程序!):

小建议:将您的问题分成多个部分(多种方法),因为如果出现错误,您可以将其缩小到少量行。

    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;
        }
    }
于 2013-09-24T07:08:59.563 回答
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;
}

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

于 2013-09-23T08:51:08.040 回答