4

I've got a WinForm application and a service that does some work from the application on a server. I want the user to able to control the service from the application, so I added a ServiceController to do all the work (Start, Stop, Restart at first only). Everything works fine so far but while testing different scenarios I encountered a problem: My service is running on a server, the application is running on a client in the same network. I connect to the service and open the ServiceController.

I then shut down the server (VM) where the service is running and trigger the stop method from the client. I use the WaitForStatus method with a timeout, problem is: the timeout is seemingly ignored by the app:

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 seems to try to stop the service for around 90 seconds and then throws an InvalidOperationException, which I can handle but I don't want the user to wait 90 seconds. I think my question basically is: What happens when the timer (30 seconds in this case) runs out? Shouldn't the code just continue to run? And when does this function throw an TimeoutException? MSDN says when "The value specified for the timeout parameter expires." - but it seems like this doesn't mean after the value reaches zero.

Can someone enlighten me?

DangerousDetlef
  • 371
  • 2
  • 11

1 Answers1

1

When we specify TimeSpan, WaitForStatus will rise timeout exception after waiting for given time but it seems to be you having exception on privileges.

please read this answer.

Community
  • 1
  • 1
  • No, it's not a problem with the privileges. I handle the authentication specifically beforehand. When the user tries the connect with the service on the remote server, I will first try to open it with the logged in credentials. If this doesn't work, it throws an InvalidOperationException, that's right - but then I will ask for different credentials from the user and try to open it again with those. If this doesn't work either, it will return with an error message. But in the case I described the credentials are right, the privileges are right but the server (the machine) is offline. – DangerousDetlef Jun 26 '15 at 12:22
  • 1
    Okay.. I'm not the expert on Services, but I've written same code to stop service and it throwing exception if time out occurred Exception message is below Message] Der Vorgang wurde nicht abgeschlossen, da der Timeout abgelaufen ist. [Source] System.ServiceProcess [StackTrace] bei System.ServiceProcess.ServiceController.WaitForStatus(ServiceControllerStatus desiredStatus, TimeSpan timeout) bei ServiceClient.ServiceClients.StopService(ServiceController service, Int32 timeOut, Int32 waitStop) [Target] Void WaitForStatus(System.ServiceProcess.ServiceControllerStatus, System.TimeSpan) – Sivakrishna Donepudi Jun 26 '15 at 12:32