Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.


YOU ARE HERE: HOME Articles Creating Windows nbsp Services nbsp with Visual Studio 2008

Creating Windows Services with Visual Studio 2008

View(s): 14137

Creating Windows Services with Visual Studio 2008

A Windows services is a application that runs in the background.  There is documentation on the Web for creating a Service with Visual Studio 2003, but is is changed for VS 2005, and 2008.

I will walk thru a sample Service in Visual Studio 2008.  This should also work with VS 2005.

Create the Service.

Description: Description: servicecreate

From Visual Studio Start Page, choose create project…   Select Windows Service and give it a name of NewService.

Description: Description: serviceblank

First thing we will do is add a timer to the Service.  DO NOT USE THE TIMER IN THE TOOLBOX.  The Timer for Windows Forms does not work for Services.  We will have to find the System.Timers.Timer component by right clicking on the Toolbox, and Choose Items.

Find the one that matches below:
Description: Description: servicetimerchoose

Click OK.  This will put the Timer into the Printing section of the Toolbar for some reason.

From The Service1 designer.  Click on Timer1, and set the interval to 10000 (10 Seconds).  Then Double-click to open Code View for Timer1_Elapsed.

Add the Following Code to the Timer1_Elapsed:

Dim MyLog As New EventLog() 
‘ create a new event log 
‘ Check if the the Event Log Exists

If Not 
MyLog.SourceExists(“NewService”) Then 
MyLog.CreateEventSource(“NewService”, “NewService_Log”) ‘ Create Log
End If 

MyLog.Source = “NewService” ‘ Write to the Log 
MyLog.WriteEntry(“NewService_Log”, “Service is Running” _ , EventLogEntryType.Information)

End Sub

And For OnStart():

Protected Overrides Sub OnStart(ByVal args() As String)

‘ Add code here to start your service. This method should set things 
‘ in motion so your service can do its work. 

Timer1.Enabled = True
End Sub

Now is normally where you would hit Run to start and debug your app.  This is not the case with Windows Services.  We have to add a few things called Installers to make them work correctly.

Creating the needed Installers

This is not the Actual “Installer”. This is code needed for the Service to operate correctly.

We will get to Installing and Deploying Later…

Add the Installer Code:

From Service1 designer, right click in the gray area and choose Add Installer.

Description: Description: serviceblank

Another file will be added to your Solution Explorer, called ProjectInstaller.  On this class will be to components in the designer, ServiceProcessInstaller1, and ServiceInstaller1.

Lets configure the Installer:  Click on the listed component and alter the following properties:


��  Account = LocalSystem   (will cause service to run under local service account)

Description: Description: serviceProperties1


·  ServiceName = NewService

·  StartType = Automatic     (Whether your service will auto start after reboot)

·  DisplayName =  NewService

Description: Description: serviceProperties2

Now we will Build and Install the service to test it out.

Build and Install your Windows Service

You cannot just hit Run and debug your Windows Service.  That would be really cool, but it is not that hard to get it running.

Build and Install service on your pc:

Choose Build in the Menubar above and choose Build NewService.

Description: Description: serviceblank

This will build your executable in the bin\release folder of your project.  If you try to run the program by itself, it will not run and give you an error.  Instead you have to load the service into your computer by using InstallUtil.exe.

You can run InstallUtil.exe from the VisualStudio Command Prompt.

Description: Description: servicecmd


InstallUtil “PATH_TO_YOUR_EXE.exe”


InstallUtil “PATH_TO_YOUR_EXE.exe” /U

When you run in the command window, you will see alot of text, and at the bottom it should read:

The Commit phase completed successfully. 
The transacted install has completed.

Now we have to start the service in Service Manager. -  Right click on My Computer on the Desktop, and choose Manage.

Description: Description: servicemanage

Choose Services and Applications, Services, and New Service:

Description: Description: servicemanager

Right click and Choose Start.

Then Go to EventViewer, Application and you should see a log for service starting, and after 10 seconds you should see your ‘Service Running” log entry
Description: Description: servicelog

Now you have a running Service.  Don’t forget to stop it.  You dont want to fill your Event log up!  We set the service to Automatic in Part 2, so you may want to uninstall using InstallUtil.exe to remove from your system.

Next we will build a Setup package so you can install easily on computers that are not running Visual Studio.

Build Setup Package for your Windows Service

If you want to install your Service on other PC’s.  The you want to make an Installer Package.

Build the Setup Package for Your Windows Service:

Prerequisites:  You will need to have .net 3.5 on the target pc.

Choose File> Add> New Project.

Description: Description: serviceblank

At the Add New Project window, Choose:

Description: Description: servicesetup1

Right click on Setup1 in Solution Explorer and choose:
Description: Description: servicesetup2

At Add Project Output Group,  It will select NewService as the Primary Output.  Just hit OK.

Then you need to tell Setup to Install the Service.  Do this by right clicking Setup1. Choose View > Custom Actions.

Then right click Custom Actions, and choose Add Custom Action.  In Select Item in Project, double click Application Folder:

Description: Description: servicesetup3

And choose OK.  It will add one item for each custom Action:

Description: Description: servicesetup4

Next, Right click Setup1 and Build.  It will create the Setup Executable in your Projects bin\release folder.

Thats It!.  Run the Setup on the Target computer, and reboot.  It will install and run the Service on reboot.

  Last updated on Friday, 21 February 2014
  Author: Mr. Ponna
2/5 stars (2 vote(s))

Register Login Ask Us Write to Us Help