Skip to content

Instantly share code, notes, and snippets.

@peteraritchie
Created November 13, 2025 23:14
Show Gist options
  • Select an option

  • Save peteraritchie/1acf6634fc4ae42b0215912ed1f42393 to your computer and use it in GitHub Desktop.

Select an option

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`
// 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