Subscribe For Free Updates!

We'll not spam mate! We promise.

Feb 17, 2013

Windows Service in C#

Views:

To day i show you how to create, install, Debug and create Setup File of a service in C#.

Windows Service Always run in Background. These    service can perform their define task continuously  with information system user .

Windows Service can Automatically start at start up of windows.We Can perform various type of task from these and windows service are already threaded mean it run parallel with windows other programs.


Creating Windows Services is not typical using C# and Visual Studio. Just follow few simple steps and you are all set to run and test your first Windows Service.
In this Service example, we writes some text to a text file when stop and start the service.  You can modify it according to your needs.

Step 1. Create Windows Service Project
Note : It preferable for Windows Service Please Open Visual Studio As Administrator Like below
Now Create windows Service Project.
Give Service Name According your Choice in my Case "VSLWindowsService"
you Will see the following window and Right click on Service.cs and Select View Code option

Set your ServiceName to your own name so it would be easier to recognize your service during testing OR you can set this property programmatically using this line this.ServiceName = "VSLWindowsService";
Example:


Default Source Code Wold be Look Like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace VSLWindowService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }
    }
}


Step 2. Add functionality to your service

As you saw VSLWindowService.cs, there are two overridden functions OnStart and OnStop. The OnStart function executes when you start your service and the OnStop function gets execute when you stop a service. I write some text to a text file when you start and stop the service.
Before this Create a folder in C: drive name temp:

protected override void OnStart(string[] args) 
{ 
FileStream fs = new FileStream(@"c:\temp\VSLWindowsService.txt" , 
FileMode.OpenOrCreate, FileAccess.Write); 
StreamWriter m_streamWriter = new StreamWriter(fs); 
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); 
m_streamWriter.WriteLine(" VSLWindowsService : Service Started \n"); 
m_streamWriter.Flush();
m_streamWriter.Close(); 
} 
///  
/// Stop this service. 
///  
protected override void OnStop() 
{ 
FileStream fs = new FileStream(@"c:\temp\VSLWindowsService.txt" , 
FileMode.OpenOrCreate, FileAccess.Write); 
StreamWriter m_streamWriter = new StreamWriter(fs); 
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); 
m_streamWriter.WriteLine(" VSLWindowsService.txt : Service Stopped \n"); m_streamWriter.Flush();
m_streamWriter.Close(); 
} 

Step 3: Install and Run the Service

We have to add an Installer to Service how to Do this See Images Below

serviceProcessInstaller1.cs> Goto Properties> SET Account=LocalSystem




We can only build this service can not run the service. For  Run this service we have to install this service.
Build of this application makes one exe, VSLWindowsService.exe. You need to call installutil to
register this service from command line.
First Move you Project Folder from Visual studio 2012/project/<your folder > Copy and Paste to D: Drive
because we install Service from Command line if folder name with space can make troble to install it so. for this cause we copy to D: drive Example

open CMD As adminstrator and Run Following commad to Install the Service

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe D:\VSLWindowService\VSLWindowService\bin\Debug\VSLWindowService.exe

FOR uninstalling Windows Service Write following Commands

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u D:\VSLWindowService\VSLWindowService\bin\Debug\VSLWindowService.exe


Service Installed Sucessfully.


Step 4: Start and Stop the Service

Now you need to go to the Computer Management to Start to start and stop the service. You can use Manage menu item by right clicking on My Computer>Select Manage.

Under Services and Applications, you will see the service VSLWindowService. Start and Stop menu item starts and stops the service.


Step 5: Test the Service

Go to your temp directory and see if text file is there with contents or not.

Download Source Code 1 (From Google)
Download Source Code 1 (From File4shared)


Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

1 comments:

  1. Quantum Binary Signals

    Get professional trading signals delivered to your cell phone daily.

    Follow our signals NOW & earn up to 270% per day.

    ReplyDelete

Become a Fan

visual studio learn