Jun 22, 2011

Create a Windows Service with Visual Studio



When you would like to start a program, users always copy the file or link the shortcut of file to the autorun directory.
In Windows, the autorun directory is located at Start Menu > All Programs > Startup

However, different users have different settings, so if another user login the Windows System, he/she will not trigger the same startup file.
Windows Systems perform some background tasks when they are booted.
Some of these tasks start before users logging in, we called them Windows Service

We can write our own Windows Service, for example auto logout when time is out.
(This service always performs in public computer like public library, but I unrecognized it must a Windows Service)

In generally, you should install the Visual Studio before.


Click File > New > Project to open a new project


Expand Visual C# (you can use other language, but this time we use C#)
And select Windows Service Visual C#
Then set the project name to Name field and click OK to start the project


Visual Studio displays like this
If the Service1.cs [Design] disappeared, double click the Service1.cs in the Solution Explorer


Click click here to switch to code view on the Service1.cs
Then change to code view of Service1.cs
There are two blanked method generated automatically by Visual Studio
protected override void OnStart(string[] args)
This method will run when this service is started
protected override void OnStop()
The method will run then this service is stopped
You also can override more methods, you can find them in MSDN
These statements are presenting a test run only, you can change them to your aim or needed
However, you cannot use any GUI libraries in Windows Service


Back to the Service1.cs [Design]
Right click any blank area in the design form then click Add Installer


Create a ProjectInstaller.cs [Design]
Click the serviceInstaller1 then right click it and click Properties (If you expand the properties window, you don't need to to it)
Modify ServiceName And StartType for your service (I suggest you don't use special character including <space>)
Automatic means when you booted the system, the service will start automaticlly
Manual means you should start the service manually
Disabled means you should enable the service before you would to start it


Click the serviceProcessInstaller1 then right click it and click Properties (If you expand the properties window, you don't need to to it)
Modify account to LocalSystem


Right click your service project and click Properties


On the properties dialog, change Startup object to your service program


Right click your solution Add > New Project


Expand Other Object Type > Setup and Development, click Setup Project
And name the setup project in Name field
Then click OK


Right click your setup project Add > Project Output


Select your service project in Project and select Primary
And (Active) in Configure
Then click OK


Right click your setup project View > Custom Actions


In Custom Actions form, right click Custom Actions and click Add Custom Action


Double click the Application Folder


Select Project Output from <your service project> (Active) then click OK


After you finished the actions before, the form presents like this


Right click your service project and click build
If you have built the service, you should click rebuild


Right click your setup project and click build
If you have built the setup, you should click rebuild


Access to the setup project directory the setup.exe file is located at debug
directory








Install the service


If you are using Windows VISTA, Windows 7, Windows Server 2008, you are able to use administrator permission to allow the installation process


Open the command line mode


If you are using Windows VISTA, Windows 7, Windows Server 2008, you are able to use administrator permission to allow the installation process


type
net start "<your service name>"
to start your service
You also can start your service in Control Panel > Administrative Tools > Services


The result represent the action when started this service


type
net stop "<your service name>"
to start your service
You also can stop your service in Control Panel > Administrative Tools > Services


The result represent the action when stopped this service


Goto Control Panel > Uninstall a Program


Find out your service setup




Click Uninstall at the center to begin the uninstall wizard


If you are using Windows VISTA, Windows 7, Windows Server 2008, you are able to use administrator permission to allow the uninstallation process


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Google Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Hostgator Coupon Code