Wednesday, June 5, 2013

Introduction to Windows Services.



Hi friends,in this post i would like to explain some basic things to know about windows services.





* A service which is under control of windows OS is called windows service.
* Windows services are used to develop automated background processes.
* Windows services contains only application logic but not GUI(Graphical User Interface).
* The controls which are not visible at runtime,can be placed in windows service projects.





For example:
Timer Control,Event log control,File system watcher control.





* To develop windows services .Net provided:
File-->NewProject-->VisualC#-->Windows-->WindowsServiceTemplate.
* Windows service project is avilable in VisualStudio .Net 2005 professional edition,but not available in Standard edition.
* To develop windows service .Net provided a name space:
System.ServiceProcess(NameSpace).
* All the windows services will be stored at:
Start-->Settings-->ControlPanel-->AdministrativeTools-->Services
OR
Start-->run-->Services.msc
* While a service is starting OnStart() event will be executed & while stoping OnStop() event will be executed.





Steps for developing Windows Service:






1)Open windows service project & wrie the required logic.
2)Add the installers & build the project to get an exe file as output.
3)Open .Net command prompt & install exefile with following syntax:
installutil -i exefile path //for installing the service.
installutil -u exefile path //for uninstalling the service.
4)Start the service.







Thank you...

Example on Windows Service



Hi Friends,in this post i would like to explain windows service for displaying message box for every 3 seconds.

Step 1:

* Open windows service project with projectName WinService1
* Goto ToolBox-->General-->Right Click-->Select choose items-->Select Timer(Systems.Timers)
* Project-->AddReference-->System.Windows.Forms(Message Box is a part of above reference).
* Place a Timer(System.Timers) with interval-3000 & enabled-false.

Code:

{//timer1_Elapsed event.
System.Windows.Forms.MessageBox.Show("This message from Windows Service.");
}

Code for OnStart() event
{
timer1.Enabled=true;
}

Code for OnStop() event
{
timer1.Enabled=false;
}





Step 2:

* Open service1.cs[design]
Right Click
Add installer(Select).
Then 2 controls will be added to the form.
1)ServiceProcessInstaller-->Choose properties-->Account=LocalSystem.
2)ServiceInstaller-->Properties-->ServiceName=MessageBoxDisplay

* Build the project(Build-->Select Build solution)

Note:
* WinService1.exe is created under:
D:\WinService1\bin\debug folder with a service name called as "MessageBoxDisplay"

Step 3:

* open .Net Command prompt.
Start-->Programs-->MSVisualStudio2005-->VSTools-->.Net Command Prompt.
>installutil -i D:\WinService1\bin\debug\WinService1.exe (press enter)
>....
>....
>....
>transaction install has completed.

Step 4:

* Open service(Start-->run-->services.msc)
* MessageBoxDisplay-->RightClick-->Properties-->Logon-->Check "interact with desktop" checkBox-->OK
(Only service contains MessageBox otherwise above step is not required).
* MessageBoxDisplay-->rightClick-->Start.

Then service will be started & MessageBox with message(This message from Windows Service.) will be displayed for every 3 seconds.


Thank You...