C# Windows Service Visual Studio 2008

Creating a program that can run in the background of the operating system has any benefits, this is very important as the program can run without a graphic user interface. Complex service that are to run are longer time, and without the need the user to login the system. Windows services are harder to develop, debug, and test. But if you software are to run in server that require to start up automatically with the server is turn on, Windows service is your choice.

Here I will demo you how to create a windows service project using C# and using .NET Framework 3.5, to cover the latest of technologies. Older framework are not much different in developing, you can find how to develop older framework Windows service from Google. I’m going to teach you how to develop a windows service

Creating your skeleton project in Visual Studio 2008

To create a Windows Service project in Visual Studio 2008, you must select from the Visual C# Windows Projects. Give your Windows Service a name, and then select OK.

As a result Visual Studio 2008 creates you an empty solution with a Windows Service project as a default to the development.

Switch to code view, to start developing.

As you can see that Visual Studio 2008 has simplify the layout and initialization of the code for Windows Service development. Now let’s start by creating the PingJob() method;

void PingJob()


while (true)




System.Net.IPHostEntry objIPHE = System.Net.Dns.GetHostEntry(“www.google.com”);

System.Threading.Thread.Sleep(15000); // wait 15 seconds


catch (Exception ex)


EventLog.WriteEntry(“PingTest Service”, ex.Message, EventLogEntryType.Warning);




As you can see that I use the method of resolving the DNS with ISP nameservers, this is more reliable method on checking the internet connection is working. And also windows service can’t output to a Windows Console to report the exception error, therefore you will have utilize method that doesn’t interact with the graphic user interface. Here I use the Windows EventLog, you can find more about this EventLog from Google (who know all).

Ok now you will have to call PingJob() method from OnStart(), I will make this PingJob() run as a separate thread from the main, and let it run until the operating system is shutdown, or the service is stopped.

void OnStart(string[] args)


System.Threading.ThreadStart job = new System.Threading.ThreadStart(PingJob);

System.Threading.Thread thread = new System.Threading.Thread(job);



void OnStop()




Do a Build Solution (Ctrl+Shift+B) to make sure that all your codes don’t have any bugs.


Creating an Installer for you Windows Service

From you solution select File>Add>New Project… from menu

Then from Add New Project, select Other Project Types, and choose Setup Project

Then Visual Studio 2008 will prepare a setup shell for you configure the set require of Solution, and now let add our Windows Service to our installer. From the Application Folder right-click, select Add>Project Output.

From the Add Project Output Group windows select the Primary output and make sure that your Windows Service Project is select has the project, then select OK.

From the Setup properties configure your properties of your installer;

Now that you would like to preconfigure your windows service application, when the user install the application into their operating system. Let us add a project installer configuration code to our project. Right-click from service class in your windows service project within design mode, and choose “Add Installer” from the menu, then ProjectInstaller class will create for you.

Switch to code mode to start configure the behavior of your windows service application, doing this by selecting ProjectInstaller.Designer.cs.

Modify the service installer as you require, as for me I usually modify account the service will run as, the service startup type, and the service name that will appear in Windows Service MMC.

void InitializeComponent()


this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();

this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();


// serviceProcessInstaller1


this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;

//this.serviceProcessInstaller1.Password = null;

//this.serviceProcessInstaller1.Username = null;

this.serviceProcessInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_AfterInstall);


// serviceInstaller1


this.serviceInstaller1.ServiceName = “PingTestService”;

this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

// ProjectInstaller


this.Installers.AddRange(new System.Configuration.Install.Installer[] {




Do a Build Solution to test that does your solution contains any bugs, let’s hope the best. Then Build the Installer from the Setup project by right-click on the Setup project, and choose build the project. Therefore you can see that there is Install/Uninstall option if you complete building the installer, and that you right-click again.

Let’s trying installing our Windows Service, as you can see that our setup wizard will show up, cool!

After you have you done.

kick it on DotNetKicks.com