One powerful feature of Visual Basic .NET is the capability to create a Windows Service. Before we start creating our own window service lets have a glance at the definition of window service.
Window Service : The core function of a windows service is to run an application in the background. There are few things that make them different from a Windows application. A Windows service starts much before any user logs in to the system (if it has been setup to start at boot up process). A Windows service can also be setup in such a way that it requires a user to start it manually – the ultimate customization!
Windows services have their own processes, and hence run very efficiently. Normally a Windows service will not have a user interface for the simple reason that it can be run even if no one is logged into the system, but this is not a rule -- you can still have a Windows service with a user interface.
you can view a list of services currently running on your computer by opening Control Panel -> Administrative Tools -> Services, as shown below:
Developing Windows Services changes completely in .NET. VB.NET is as good a choice as any other .NET language for developing a Windows Service. As with many areas of advanced functionality, the reason you can use VB.NET for this purpose is because base classes in the .NET Framework take care of the system-level interfacing. You need two base classes to create a Windows Service in VB.NET: System.ServiceProcess.ServiceBase and System.Configuration.Install.Installer.
System.ServiceProcess.ServiceBase provides the base class for the service itself. The class that inherits from this class contains the logic that runs in the service. System.Configuration.Install.Installer provides the interface necessary to get the service installed under Windows 2000 and NT. It isn't typically necessary to put any logic in your installer class.
Create the Beep Service example
Create a new project in Visual Studio with the type Windows Service. Name the project BeepService, then go to the design surface for the Service1 component. Open the Toolbox and click on the Components tab. Drag a Timer control from the Toolbox onto the Service1 component. It appears on the design surface with the name Timer1.
In the Properties window for Timer1, change the Interval property to a value of 3000. (i.e; 3,000 milliseconds, which causes the timer to fire every three seconds.) Then go back to the code for Service1.vb. Enter this code in the OnStart event:
Install the Beep Service
You must start the .net utility InstallUtil.exe from a command line to install the window service. InstallUtil.exe is located in "c:\windows\Microsoft.NET\Framework\v2.0.50727". Start up a DOS-style command window or Visual Studio.NET command Window and change the current directory to
c:\windows\Microsoft.NET\Framework\v2.0.50727.
Start BeepService
Now goto Control Panel -> Administrative Tools -> Services as shown above, Note the presence of the BeepService program you installed earlier; if BeepService doesn't appear, the installation wasn't successful.
Window Service : The core function of a windows service is to run an application in the background. There are few things that make them different from a Windows application. A Windows service starts much before any user logs in to the system (if it has been setup to start at boot up process). A Windows service can also be setup in such a way that it requires a user to start it manually – the ultimate customization!
Windows services have their own processes, and hence run very efficiently. Normally a Windows service will not have a user interface for the simple reason that it can be run even if no one is logged into the system, but this is not a rule -- you can still have a Windows service with a user interface.
you can view a list of services currently running on your computer by opening Control Panel -> Administrative Tools -> Services, as shown below:
Developing Windows Services changes completely in .NET. VB.NET is as good a choice as any other .NET language for developing a Windows Service. As with many areas of advanced functionality, the reason you can use VB.NET for this purpose is because base classes in the .NET Framework take care of the system-level interfacing. You need two base classes to create a Windows Service in VB.NET: System.ServiceProcess.ServiceBase and System.Configuration.Install.Installer.
System.ServiceProcess.ServiceBase provides the base class for the service itself. The class that inherits from this class contains the logic that runs in the service. System.Configuration.Install.Installer provides the interface necessary to get the service installed under Windows 2000 and NT. It isn't typically necessary to put any logic in your installer class.
Create the Beep Service example
Create a new project in Visual Studio with the type Windows Service. Name the project BeepService, then go to the design surface for the Service1 component. Open the Toolbox and click on the Components tab. Drag a Timer control from the Toolbox onto the Service1 component. It appears on the design surface with the name Timer1.
In the Properties window for Timer1, change the Interval property to a value of 3000. (i.e; 3,000 milliseconds, which causes the timer to fire every three seconds.) Then go back to the code for Service1.vb. Enter this code in the OnStart event:
Timer1.Enabled = True
Enter this code in the OnStop event for the class:
Timer1.Enabled = FalseTo create an Elapsed event for the timer, highlight Timer1 in the left-hand dropdown box at the top of the code editor window, then select the Elapsed event in the right-hand dropdown. Place this line of code in the Elapsed event:
Beep()
Now add an installer to the project. Go back to the design surface for Service1 and right-click on it. Select Add Installer, which creates a new component called ProjectInstaller and adds it to the project.
Highlight the ServiceProcessInstaller1 control on the ProjectInstaller design surface. Change the Account property in its Properties window to LocalSystem. Then highlight the ServiceInstaller1 control. In its Properties window, change the ServiceName property to BeepService. Now build the project. This creates an EXE for the service.
Now you're ready to install the service. After getting one of the two command windows, enter a command such as this:
InstallUtil.exe {path for BeepService.exe}
The path for the Windows Service executable varies depending on where you put your BeepService project. Remember that .NET places executable modules in a \bin subdirectory under the project directory. If the executable's path is complex, you might want to copy the executable to a simpler path such as C:\MyServices. In this case, the command to install the service would look like this:
InstallUtil.exe
C:\MyServices\BeepService.exe
You should look at the messages generated by InstallUtil.exe to ensure the service installed successfully.
Start BeepService
Now goto Control Panel -> Administrative Tools -> Services as shown above, Note the presence of the BeepService program you installed earlier; if BeepService doesn't appear, the installation wasn't successful.
Start BeepService by right-clicking on it and selecting Start. Your computer begins to beep every three seconds. You can stop the service by right-clicking on BeepService and selecting Stop.
Uninstalling the serviceUninstalling the service is similar to installing it. You do this in a command window; use the same command used as the one for installation, except include the option "/u" just before the path for the service. Assuming you placed BeepService.exe in the C:\MyServices directory, the command to uninstall BeepService would look like this:
InstallUtil.exe /u
C:\MyServices\BeepService.exe
Once you uninstall BeepService, it no longer shows up in the list of available services to start and stop.
You must uninstall and reinstall a Windows Service every time you make changes to it. Important: You should close the Services screen every time you uninstall a service. If the Services screen remains open, reinstalling your service might be difficult. After reinstalling the service, you can reopen the Services program from Administrative Tools.
Hope this article be usefull...
