using System;
using System.ServiceProcess;
using System.Threading;
public class MyBackgroundService : ServiceBase
{
private Thread _workerThread;
private bool _isRunning;
protected override void OnStart(string[] args)
{
_isRunning = true;
_workerThread = new Thread(DoWork);
_workerThread.Start();
}
protected override void OnStop()
{
_isRunning = false;
_workerThread.Join();
}
private void DoWork()
{
while (_isRunning)
{
Console.WriteLine("Background service is running...");
Thread.Sleep(1000);
}
}
public static void Main(string[] args)
{
ServiceBase.Run(new MyBackgroundService());
}
}