Tag Archives: C#

Run only one instance of a program in C#

This example shows you how to ensure that only one instance of an application is running. // Ensure only one instance of this application is running. bool alone = false; Mutex m = new Mutex(true, "<name>", out alone);   if (!alone) { Console.WriteLine("Another instance is already running."); Console.ReadKey(); return; }   Console.WriteLine("Running…\n"); Console.ReadKey();   // [...]

Restart a Windows service using C#

This example shows how to start, stop and restart a windows service programmatically in C#. Start service The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs. public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); [...]