.NET ServiceController.WaitForStatus ignores timeout

I have a WinForm application and a service that does some work with the application on the server. I want the user to be able to control the service from within the application, so I added a ServiceController to do all the work (Start, Stop, Restart first). Everything is working fine so far, but while testing various scenarios I ran into a problem: my service is running on the server, the application is running on the client on the same network. I connect to the service and open the ServiceController.

Then I shutdown the server (VM) where the service is running and run the stop method from the client. I am using WaitForStatus method with a timeout, the problem is this, the timeout appears to be ignored by the application:

public void StopService()
    {
        if (this._serviceController.CanStop &&
            (this.ServiceStatus == ServiceControllerStatus.Running || this.ServiceStatus == ServiceControllerStatus.Paused))
        {
            this._serviceController.Stop();
            this._serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
        }

    }

      

In my case, the methods seem to try to stop the service for about 90 seconds and then throw an InvalidOperationException that I can deal with, but I don't want the user to wait 90 seconds. I think my question is basically this: what happens when the timer (30 seconds in this case) ends? Shouldn't the code keep working? And when does this function throw a TimeoutException? MSDN says "The value specified for the timeout parameter expires." - but that doesn't seem to mean that after the value reaches zero.

Can someone enlighten me?

+3


source to share


1 answer


When we specify a TimeSpan, WaitForStatus will increase the timeout after waiting for the specified amount of time, but it looks like you have a privilege exception.



read this answer .

+1


source







All Articles