I found the following code on the Internet:
private static ServiceController ser { get; set; } using (ser = new ServiceController("MpsSvc")) { ser.Start(); label.Visible = true; label.Text = "Служба запущена!"; ser.Close(); } Sometimes it works, sometimes it doesn't.
Can the service existence check + service start check be screwed to the above code (if the service is stopped to start)
Should I use get;set; for such purposes?
Redid so correct if not correct)
private void StopService_Click(object sender, EventArgs e) { using (var serviceController = new ServiceController("sevice")) { if (CheckService.CheckIfServiceExists("sevice")) { serviceController.Stop(); serviceController.WaitForStatus(ServiceControllerStatus.Stopped); label.Visible = true; label2.Text = "Служба остановлена"; StopService.Visible = false; StartService.Visible = true; } } } The first time passes normally, the next one says that the service could not be stopped / started (although the service is running before stopping)
What is the reason?