I’m getting a service controller for one of my services:
foreach (ServiceController sc in ServiceController.GetServices())
{
if (sc.ServiceName == ConfigurationManager.SYNC_SERVICE_NAME)
{
this._serviceController = sc;
break;
}
}
and then calling the status function on it when the user triggers an action:
private void tsmiStartServer_Click(object sender, EventArgs e)
{
if (_serviceController.Status == ServiceControllerStatus.Stopped)
this._serviceController.Start();
}
However, even when the service is stopped, the status will show up as running. In fact, the status does not seem to change at all leading me to think it is detected and cached at the point I get the service controller object.
However, I’ve found that if I put a Refresh() call before the call to the Status property, it will detect the correct state of the service. This seems to contradict what the doco says about the call “Gets the status of the service that is referenced by this instance.” unless I’m no longer referencing the correct instance somehow… ??
You should call the
Refreshmethod on theServiceControllerto ensure that you have the current state: