Your intention is mostly correct, but looks like there's a logical mistake in your condition. You're checking:
MyService.CanStop = True AndAlso MyService.Status <> ServiceControllerStatus.Running
This means only stop the service if it can be stopped AND it's not running, which doesn't seem to make much sense, because ServiceController.Stop()
only applies to a running service. Instead, you should only stop the service if it's running and can be stopped. So, your condition should be:
If MyService.CanStop AndAlso MyService.Status = ServiceControllerStatus.Running Then
MyService.Stop()
End If
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin