Creating a Counter Monitor Service


To illustrate the outlined steps, the following example creates a simple service that checks the value of a performance counter, and when the value of the counter exceeds a certain value, the service beeps every three seconds. This is a good example for stepping through the process of creating, installing, and starting a Windows Service. It contains very little logic, and you can easily tell when it is working.

In the first phase of the example, you create a service that always beeps. Then, in the second phase, you add logic to monitor the performance counter and only beep when the counter exceeds a specific value:

  1. Start a new Windows Service project using Visual Studio 2005. Name the project CounterMonitor.

  2. In the Solution Explorer, rename Service1.vb to CounterMonitor.vb.

  3. Click the design surface for CounterMonitor.vb. In the Properties window, change the ServiceName property from Service1 to CounterMonitor (the Name property changes the name of the class on which the service is based, while the ServiceName property changes the name of the service as known to the Service Control Manager).

  4. Right-click the project for the service and select Properties. You will then be presented with the CounterMonitor Property Pages as one of the paged tabs directly in Visual Studio. From the Application tab, set the Application Type drop-down list to Windows Service if necessary (it should already be set to this) and from the drop-down list named Startup Object, make sure CounterMonitor is selected (see Figure 31-2).

    image from book
    Figure 31-2

  5. Go back to the CounterMonitor.vb file’s design view and open the Visual Studio 2005 Toolbox. Open the Components (not the Windows Forms) node in the Toolbox. Drag a Timer control from the Toolbox onto the CounterMonitor design surface. It will appear on the design surface with the name Timer1. It is very important that you grab the correct Timer object from the Toolbox. Visual Studio 2005 does not have the Timer object for this example in the Toolbox, whereas earlier versions of Visual Studio did. To ensure you have the correct Timer object, right-click on the Toolbox and select Choose Items from the menu. In the .NET Framework Components tab of the Choose Toolbox Items dialog, scroll down until you see a couple of Timer objects. In the list are a couple of Timer objects from the System.Windows.Forms namespace, but you want to instead choose the Timer object that is part of the System.Timers namespace. This is the Timer object that you want to work with for the rest of this example.

  6. In the Properties window for Timer1, change the Interval property to a value of 3000 (that’s 3,000 milliseconds, which causes the timer to fire every three seconds).

  7. Go to the code for CounterMonitor.vb. Inside the OnStart event handler (which is already created for you in the code), enter the following:

      Timer1.Enabled = True 

  8. In the OnStop event for the class, enter the following:

      Timer1.Enabled = False 

  9. Create an Elapsed event for the timer by highlighting Timer1 in the left-hand drop-down box at the top of the code editor window. Then, select the Elapsed event in the right-hand drop-down box from the code view of the file.

  10. In the Elapsed event, place the following line of code:

      Beep() 

  11. Now add an installer to the project. Go back to the design surface for CounterMonitor and right-click it. Select Add Installer. A new file called ProjectInstaller1.vb is created and added to the project. The ProjectInstaller1.vb file will have two components added to its design surface, ServiceProcessInstaller1 and ServiceInstaller1, as shown in Figure 31-3.

    image from book
    Figure 31-3

  12. On the ProjectInstaller.vb design surface, highlight the ServiceProcessInstaller1 control. In its Properties window, change the Account property to LocalSystem.

  13. Highlight the ServiceInstaller1 control. In its Properties window, type in CounterMonitor as the value of the DisplayName property.

  14. Now build the project by right-clicking on the solution and selecting Build from the menu. An EXE named CounterMonitor.exe will be created for the service.

Installing the Service

Now you are ready to install the service. The utility for doing this, InstallUtil.exe, must be run from a command line. It is located in the .NET utilities directory, found at C:\WINNT\Microsoft.NET\ Framework\v2.0.50727 on Windows 2000 and NT systems, or C:\Windows\Microsoft.NET\ Framework\v2.0.50727 on Windows XP and Windows 2003.

You can easily access this utility (and all the other .NET utilities in that directory) using an option from the Programs menu that is installed with Visual Studio 2005. Choose Microsoft Visual Studio 2005 image from book Visual Studio Tools image from book Visual Studio 2005 Command Prompt. In the command window that appears, change to the directory that contains CounterMonitor.exe. By default, when using Visual Studio 2005, you’ll find this executable at C:\Documents and Settings\[ user ]\My Documents\Visual Studio\Projects\CounterMonitor\Projects\CounterMonitor\CounterMonitor\obj\Debug. Once found, run the following command:

 InstallUtil CounterMonitor.exe

Check the messages generated by InstallUtil.exe to ensure that the installation of the service was successful. The utility generates several lines of information; if successful, the last two lines are as follows:

 The Commit phase completed successfully. The transacted install has completed.

If these two lines do not appear, you need to read all the information generated by the utility to find out why the install did not work. Reasons might include a bad pathname for the executable, or trying to install the service again when it is already installed (it must be uninstalled before it can be reinstalled), as described later.

Starting the Service

Later in this chapter, you will create your own “control panel” screen to start and stop the service. For now, to test the new Windows Service, you will use the Server Explorer in Visual Studio 2005. Open the Server Explorer in Visual Studio 2005 and expand the Services node. The resulting screen is shown in Figure 31-4.

image from book
Figure 31-4

If the CounterMonitor service does not appear in the list, the installation was unsuccessful. Try the installation again and check the error messages. Right-click the CounterMonitor service. Select the Start menu option. You will hear the service beep every three seconds. You can stop the service by right-clicking it again and selecting the Stop menu option.

You can also use the Service Control Manager built into Windows to start the CounterMonitor service, shown in Figure 31-5.

image from book
Figure 31-5

Start CounterMonitor by right-clicking it and selecting Start or by clicking the Start link. As before, you will hear your computer beep every three seconds. Stop the service by right-clicking CounterMonitor and selecting Stop or by clicking the Stop link. Note that if you already started the service via the Server Explorer (as described earlier), then it will be in a Started state when you access the Service Control Manager program.

Uninstalling the Service

Uninstalling the service is very similar to installing it. The service must be in a stopped state before it can be uninstalled, but the uninstall operation will attempt to stop the service if it is running. The uninstall operation is done in the same command window (with the Visual Studio 2005 Command Prompt) as the install operation, and the command used is the same as the one for installation, except that the option /u is included just before name of the service. Remember that you need to navigate to C:\Documents and Settings\[ user ]\My Documents\Visual Studio\Projects\ CounterMonitor\Projects\CounterMonitor\CounterMonitor\obj\Debug to run this ommand:

 InstallUtil.exe /u CounterMonitor.exe

You can tell that the uninstall was successful if the information displayed by the utility contains the following line:

 Service CounterMonitor was successfully removed from the system.

If the uninstall is unsuccessful, read the rest of the information to find out why. Besides typing in the wrong pathname, another common reason for failure is trying to uninstall a service that is in a running state and could not be stopped in a timely fashion.

Once you have uninstalled CounterMonitor, it will no longer show up in the list of available services to start and stop (at least, after a refresh it won’t).

Important 

A Windows Service must be uninstalled and reinstalled every time you make changes to it. You should uninstall CounterMonitor now because we’re about to add new capabilities to it.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net