Hi @Sphesihle Jamile
To stop and start a Windows background service using C#, you can use the ServiceController class, and then use the Start() and Stop() method.
According to this tutorial: Create Windows Service using BackgroundService, I create two windows services: WorkerSerivce1 and WorkerService2, then in the third application: console application, we can use the following code to start and stop windows services:
using System.ServiceProcess;
ServiceController service1 = new ServiceController("WorkerService1 Service");
// Start the service
service1.Start();
Console.WriteLine("WorkerService1 start");
Thread.Sleep(20000);
// Stop the service
service1.Stop();
Console.WriteLine("WorkerService1 stop");
ServiceController service2 = new ServiceController("WorkerService2 Service");
service2.Start();
Console.WriteLine("WorkerService2 start");
Thread.Sleep(20000);
service2.Stop();
Console.WriteLine("WorkerService2 stop");
The result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion