Creating Your First Windows Services Project

Open Visual Studio .NET and create a Windows Services project. Name the project LearningVBservice. You'll see that all you have in the Solution Explorer is a .VB file named Service1, along with the AssemblyInfo.vb file.

Click once on the designer to access the properties for the service. Change the ServiceName property from Service1 to UsageMonitor. Also change the (Name) property from Service1 to UsageMonitor. Now, double-click on the designer to access the code. You'll notice that the first line after the Imports statement is this:

Public Class UsageMonitor

If you expand the code the IDE generated for you, you'll notice that you have a Sub New, which is not surprising. However, you also have a Shared Sub Main, which is not something you have seen in most of your other applications. As you might suspect, the Shared Sub Main is the first procedure that will run by default. Therefore, there is already some code in this routine to initialize the service. One line of code in Shared Sub Main references Service1, but you've changed that name. Therefore, find the following line of code:

ServicesToRun = New System.ServiceProcess.ServiceBase() { New Service1()}

Change the code to this:

ServicesToRun = New System.ServiceProcess.ServiceBase() { New UsageMonitor()}

Now, go back to the designer page. Open the Toolbox and in the Components tab, drag a Timer over and drop it on the designer. The entire Windows service is a component, which as you have seen, has a form-like designer. Because this designer is not a form, the Timer does not end up in the component tray; instead, VS .NET shows the control on the designer surface.

If you select the Timer1 component and look at its properties, you'll see that by default it is enabled, and its Interval property is set to 100. The Interval property is in milliseconds, and is of type Double. Therefore, to wait one second, you put in a value of 1000. For this example, you want to capture statistics once every five seconds, so enter 5000 in the Interval property. In a real application, you might want to grab values only every minute or every five minutes, but you probably don't want to sit and wait ten minutes just to make sure that this first Windows Services project works properly.

Note

The Enabled property of the Timer has switched from being True or False numerous times throughout the beta process. Therefore, it is entirely possible that timers will be disabled by default in the final release of VS .NET.


Next, drag over a PerformanceCounter control from the Components tab of the Toolbox. This allows you to access the same performance information you can get with the Performance tool (often called Performance Monitor, or PerfMon). Highlight the PerformanceCounter1 control and modify the following properties:

  • Set CategoryName to "Processor"

  • Set CounterName to "% Processor Time"

  • Set InstanceName to "_Total"

  • Leave MachineName set to a period, which is a shortcut for the local machine name

Note

An alternative approach is to drag over a PerfCounter object from the Server Explorer, and then tweak the properties. Don't forget about the Server Explorer when working with services.


Double-click on the Timer1 component to open the code window. By default, the IDE puts you in the Timer1_Elapsed event procedure. This isn't what you want, because you want to respond to an event every time the timer fires. Therefore, you want to work with the Timer1_Tick event procedure. Before entering anything for the Timer1_Tick procedure, go to the very top of the code window and add the following line of code:

Imports System.IO

Now, create the Timer1_Tick event procedure and make it look like the following code:

Protected Sub Timer1_Tick(ByVal sender As Object, _
  ByVal e As System.EventArgs)
   Dim file As TextWriter = New StreamWriter("c:output.txt", True)
   file.WriteLine("CPU Usage: " & _
     PerformanceCounter1.NextValue.ToString & " - " & Now)
   file.Close()
End Sub

This is the code that runs every five seconds. It opens a text file called output.txt in the root of the C: drive. The True argument of the WriteLine method appends to the existing file. The code writes a string that contains the CPU usage from the performance counter, and then appends the current date and time. Finally, it closes the file. Notice that to use the shortcut name StreamWriter, you had to add the System.IO namespace, which has classes that handle many different types of I/O operations. This keeps you from having to type System.IO.StreamWriter to reference the class.

If you look further down in the code, you should see stubs for the OnStart and OnStop routines. Modify the stubs—and add an OnContinue routine—as shown in the following:

Protected Overrides Sub OnStart(ByVal args() As String)
    timer1.Enabled = False
    Dim file As TextWriter = New StreamWriter("c:output.txt", True)
    file.WriteLine("Service Started")
    file.Close()
    timer1.Interval = 5000
    timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop()
    Timer1.Enabled = False
    Dim file As TextWriter = New StreamWriter("c:output.txt", True)
    file.WriteLine("Service Stopped")
    file.Close()
End Sub

Protected Overrides Sub OnContinue()
    Dim file As TextWriter = New StreamWriter("c:output.txt", True)
    file.WriteLine("Service Restarted")
    file.Close()
End Sub

As you can see in the OnStart procedure, when the service starts, the code writes to the text file that the service is starting. Then the timer's interval is set and the timer is enabled, just to make sure that the settings are correct. In the OnStop procedure, you record the fact that the service is stopped if someone does stop the service. If someone pauses the service and later resumes it, the fact that the service was restarted is written in the text file in the OnContinue procedure.

This is all that needs to be written in the service. However, you have to do more to make the service usable: You have to add installers to your application.

Adding Installers to Your Service

You need to return to the designer for your service. Right-click on the designer and choose Add Installer. A new window is added to the designer area, labeled ProjectInstaller.vb [Design]. Two components will be added to the project. One, the ServiceInstaller1, is for installing your service, whereas the other, ServiceProcessInstaller1, is for installing the process that hosts your service while it is running. All services run in their own process so that they can run independently of any particular user. Both the ServiceInstaller and the ServiceProcessInstaller are used by an installation utility to properly register and configure the service.

Before building the project, you need to perform a couple of tasks because you changed the name of the class from UserService1 to UsageMonitor. On the ProjectInstaller.vb designer, click on the ServiceInstaller1 control and verify that the ServiceName property is set to UsageMonitor, changing it if necessary. Then, in the Solution Explorer window, right-click on the LearningVBservice project and choose Properties. In the property pages, change the Startup Object combo box to UsageMonitor and click OK.

Next, click on the ServiceProcessInstaller1 and look at the Account property. The Account property determines the type of account under which this service will run. The default is User, but the Username and Password properties are blank by default. If you leave the Username and Password properties blank, the installation (not the build) will fail. In certain interim builds of VS .NET, installing a service without the Username and Password defined causes a box to prompt you for the necessary information. This is no longer the case, although it is still documented, so you will have to either enter a Username and Password, or change the type of account under which you want the service to run.

In this example, simply change the Account property to LocalSystem. This will allow the service to run under the local system account and will obviate the need for you to enter a hard-coded Username and Password.

You are now ready to build the service. Choose Build from the Build menu. Your service will now be built.

Configuring Your Service

Your service is compiled, but the system doesn't know anything about it yet. You have to install this service before it will be available for starting and stopping in the Service Control Manager. Open a command or console window (that's a DOS prompt), and go to the directory where the service was compiled. For most of you, this will be C:Documents and Settings<your username>My DocumentsVisual Studio ProjectsLearningVBservicein. When you're there, type in the following command:

installutil LearningVBservice.exe

Note

InstallUtil.exe must be in the path in order to work in this example. If it is not in the path, you'll have to type the full pathname.


This will take a moment. After the installation is done, you are ready to start your service and set its startup options.

Open the Services management tool, which can be found in the Computer Management application in Windows 2000. Your service, UsageMonitor, will appear in the list, as you see in Figure 10.1. Double-click the UsageMonitor service to open the Properties dialog box. You can change the startup type from Manual to Automatic if you want the service to start every time Windows boots up, and it will launch and begin operations even if no one logs on. At this point, you can just click the Start button and let the service run for a while. After it runs for a time, stop it. Now you can examine the log that it produced.

Figure 10.1. Your first Web service showing up in the Windows Services Control Manager.


The log file that is produced shows the CPU utilization every five seconds. The service can only be stopped and started. It cannot be paused because the CanPauseAndContinue property is set to False by default, and you did not change that before you built the service. Here is a sample of the file that was produced:

Service Started
CPU Usage: 0 - 2/24/2001 4:21:55 PM
CPU Usage: 9.504132 - 2/24/2001 4:21:57 PM
CPU Usage: 9.8 - 2/24/2001 4:22:02 PM
...
CPU Usage: 23 - 2/24/2001 4:24:07 PM
CPU Usage: 1.19760478 - 2/24/2001 4:24:12 PM
CPU Usage: 5.61122227 - 2/24/2001 4:24:17 PM
CPU Usage: 4.2 - 2/24/2001 4:24:22 PM
Service Stopped

The first value for the CPU usage is often a throwaway value. After that, all the numbers look legitimate.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.105.83