Windows Services  

How to Create a Windows Service Using C#

Windows Services are background applications that run without UI and start automatically with Windows. They are ideal for:

✔ Scheduled tasks
✔ File monitoring
✔ Background processing
✔ Automation jobs
✔ Queue listeners

Step 1: Create the Windows Service Project

In Visual Studio

  1. Open Visual Studio

  2. Click Create a new project

  3. Search for Windows Service (.NET Framework)
    (Use .NET Framework version 4.7.2+)

  4. Give it a name → e.g., MyWindowsService

  5. Click Create

You will see a class called:

public partial class Service1 : ServiceBase

Step 2: Write Service Code

Open Service1.cs → write logic inside OnStart and OnStop.

public partial class Service1 : ServiceBase
{
    private System.Timers.Timer timer;

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer = new System.Timers.Timer();
        timer.Interval = 5000; // runs every 5 seconds
        timer.Elapsed += Timer_Elapsed;
        timer.Start();
    }

    private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        WriteLog("Service is running at: " + DateTime.Now);
    }

    protected override void OnStop()
    {
        WriteLog("Service stopped.");
    }

    private void WriteLog(string message)
    {
        string path = @"C:\MyServiceLog.txt";
        File.AppendAllText(path, message + Environment.NewLine);
    }
}

Step 3: Add Installer (Required!)

Right-click inside Service1.csAdd Installer

It will generate two components:

ServiceProcessInstaller
ServiceInstaller

Set properties

ServiceProcessInstaller

Account = System.ServiceProcess.ServiceAccount.LocalSystem;

ServiceInstaller

ServiceName = "MyWindowsService";
StartType = Automatic;

Step 4: Build the Project

Go to:

Build → Build Solution

It creates .exe inside:

bin\Release\

Step 5: Install the Service (Windows CMD)

Open Command Prompt as Administrator.

Run:

sc create MyWindowsService binPath= "C:\Path\To\MyWindowsService.exe"

Start the service:

sc start MyWindowsService

Stop the service:

sc stop MyWindowsService

Delete service:

sc delete MyWindowsService

Alternative Installation (PowerShell)

New-Service -Name "MyWindowsService" -BinaryPathName "C:\Path\MyWindowsService.exe"
Start-Service MyWindowsService

Step 6: Debugging Windows Service

Windows services cannot run by double-click.
To debug:

Option 1 — Temporary Console Mode

Add this code:

#if DEBUG
System.Threading.Thread.Sleep(10000);
#endif

Or create a console runner.

Option 2 — Attach to Process

  1. Install service

  2. Start service

  3. In Visual Studio → Debug → Attach to Process

  4. Select your service EXE

  5. Add breakpoints → Debug

Top Use Cases

Use CaseWhy Windows Service?
Auto-backup jobsRuns even when user is logged out
File monitoringContinuous background watch
Sending emailsNo UI required
Data sync jobsRuns at startup
Long-running jobsStable background processing