Created
November 13, 2025 23:14
-
-
Save peteraritchie/1acf6634fc4ae42b0215912ed1f42393 to your computer and use it in GitHub Desktop.
#hypothesis a small program to stop a windows service and restart it when service status is ` Stopped`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Example in helper process (e.g., RestartService.exe) | |
| using System.ServiceProcess; | |
| using System.Threading; | |
| public class Program | |
| { | |
| public static void Main(string[] args) | |
| { | |
| if (args.Length > 0) | |
| { | |
| string serviceName = args[0]; | |
| ServiceController service = new ServiceController(serviceName); | |
| try | |
| { | |
| if (service.Status != ServiceControllerStatus.Stopped) | |
| { | |
| service.Stop(); | |
| service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); // Wait for stop | |
| } | |
| service.Start(); | |
| service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30)); // Wait for start | |
| } | |
| catch (Exception ex) | |
| { | |
| // Log error | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment