How to Stop and Start a Windows Background Service

Sphesihle Jamile 0 Reputation points
2023-08-14T09:33:22.4966667+00:00

I have multiple ASP.NET Core Worker Services that run on my server and want to create another project for database cleanup. How can I turn a Windows background service on and off using C# for this purpose?

Good day guys,

I have created multiple ASP.NET Core Worker Services that run on my server. Let's name these background/worker services:

  1. workerService1.exe
  2. workerService2.exe

workerService1.exe's job is to scan for data that is being sent to a certain network port. This data is then decoded, converted into various data types and objects, and then saved to the database. workerService2.exe then runs and collects all of the data from the database, performs some algorithm on that data, and sends the results to an API hosted on another service. These two services are made separate because they run on different schedules and they perform different functions. There are usually 10 to 20 devices that send data to the network port that workerService1.exe is scanning.

I want to create workerService3.exe which is only going to focus on database cleanup. Once data is saved into the database, it is only valid for about 24 hours. Since I created these projects, I've had to do manual database cleanups to make sure that the services do not consume too much space in the database. How, through C#, can I switch a Windows background service on and off? workerService1.exe, workerService2.exe and workerService3.exe are three separate projects

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,391 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,101 Reputation points Microsoft Vendor
    2023-08-15T05:42:46.63+00:00

    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:

    image1


    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

    0 comments No comments