- Back to Home »
- Windows Applications »
- Creating Windows Services
Posted by :
Sudhir Chekuri
Thursday, 10 March 2016
File – New Project – Select language as C# and windows service as application type.
Enter name for the project. Example: SampleWindowsService
Click on ok.
Right-click the "Service1.cs" file in Solution Explorer and rename it "to" Scheduler or any other name that you want to give it. Then click “click here to switch to code view”.
In the code view, you can see two methods called OnStart() and OnStop(). The OnStart() triggers when the Windows Service starts and the OnStop() triggers when the service stops.
Create a new class file(Library.cs) in the project with a method to log messages using below code.
Code in Library.cs file
using System;
Enter name for the project. Example: SampleWindowsService
Click on ok.
Right-click the "Service1.cs" file in Solution Explorer and rename it "to" Scheduler or any other name that you want to give it. Then click “click here to switch to code view”.
In the code view, you can see two methods called OnStart() and OnStop(). The OnStart() triggers when the Windows Service starts and the OnStop() triggers when the service stops.
Create a new class file(Library.cs) in the project with a method to log messages using below code.
Code in Library.cs file
using System;
using System.IO;
namespace SampleWindowsService
{
public static class Library
{
public static void WriteErrorLog(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory+"LogFile.txt",true);
sw.WriteLine(DateTime.Now.ToString() + ": " + Message);
sw.Flush();
sw.Close();
}
catch { }
}
}
}
Scheduler.cs
using System;
using System.ServiceProcess;
using System.Text;
using System.Timers;
namespace SampleWindowsService
{
public partial class Scheduler : ServiceBase
{
private Timer timer1 = null;
public Scheduler()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 30000;//every 30 secs
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
Library.WriteErrorLog("Test window service
started");
}
private void timer1_Tick(object sender, ElapsedEventArgs e)
{
//Write code her to do some scheduled task
Library.WriteErrorLog("Job is running");
}
protected override void OnStop()
{
timer1.Enabled = false;
Library.WriteErrorLog("Test window service
stopped");
}
}
}
Go to design view of Scheduler.cs – right click on the design window and select Add Installer.
Now ProjectInstaller.cs file will be added to your project in solution explorer.
Open ProjectInstaller design window – Right click on serviceInstaller1 – Select properties
Change StartType property to automatic so that service will be automatically started.
Open ProjectInstaller design window – Right click on serviceProcessInstaller1 – Select properties
Change Account property to LocalSystem.
Build the project and see the exe file created in project location.