articles

Home / DeveloperSection / Articles / Windows Service

Windows Service

Anonymous User12509 30-Jul-2010

Windows services don’t have any user interface, when we want to create any long running code we create windows services which runs in background. It has the capability to start automatically when computer boots.

Example to demonstrate how to create Windows Service and install

To create Windows Service open new project and form new project dialog box select “Windows Service”.

Windows Service

When you press OK the result will look like this

Windows Service

By default, the wizard adds “Service1.cs”; rename it to the name you desire your service to be. Here I’ve named it to “SampleWinService.cs”. By default two overridden method are declared “OnStart” and “OnStop”

Now for coding switch to code view.

Code
//here we are creating file so add “System.IO;” namespace to your project.
//overriding OnStart method
 
protectedoverridevoid OnStart(string[] args)
        {
//creating file stream to create log file of the event.
            string Filename = @"c:\sample.log";
            FileStream fs = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter sw = newStreamWriter(fs);
//writing to file the detail of event occurred.
            sw.WriteLine("Process started at :" + System.DateTime.Now.ToString());
            sw.Close();
            fs.Close();
        }
 
        protectedoverridevoid OnStop()
        {
            string Filename = @"c:\sample.log";
            FileStream fs = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter sw = newStreamWriter(fs);
            sw.WriteLine("Process ended at :" + System.DateTime.Now.ToString());
            sw.Close();
            fs.Close();
        }

Now once the code for windows service is finished. Add a ProjectInstaller to your project.

To add project installer right click on the design view of the the file and select “Add Installer”.

Windows Service

A new file “ProjectInstaller.cs” will be added to your project. When the file is created two components (ServiceProcessInstaller, ServiceInstaller) are already added to the file.

Windows Service

Now select ServiceProcessInstaller1 and change its Account property in property explorer to “Local System”.

Windows Service

Now select ServiceInstaller1 and change its “Start Type” property to “Automatic” and “Service Name” property to the desired name you want to be displayed. Here I’ve named it to SampleWinService.

Windows Service

Now build the project.

Once the project is built successfully we need to install the service.

Installing the service

To install the service open “Visual Studio Command Prompt” as an Administrator. Now to install the service use “Installutil” command with the file name of the service created after building your project, situated in  “..\bin\debug” folder of the project, with its path.

Example

InstallUtill ..\bin\debug\SampleWinService.exe

(‘..’ in the example means the root directory, don’t use ‘..’ write the full path)

This will install the service to your computer.  To check your service write “compmgmt.msc” in Run Prompt, Computer Management Dialog Box will appear. In that select Service under Services and Applications Option.

Windows Service

Here you can see your service running.

To uninstall your service, in the Command Prompt write

Installutil ..\bin\debug\SampleWinService.exe /u


I am a content writter !

Leave Comment

Comments

Liked By