Custom Commands


Some services need additional operations besides starting and stopping. For example, for the CounterMonitor Windows Service, you might want to set the threshold value of the performance counter that causes the service to begin beeping, or you might want to change the interval between beeps.

With most components, you would implement such functionality through a public interface. That is, you would put public properties and methods on the component. However, you cannot do this with a Windows Service because it has no public interface that you can access from outside the service.

To deal with this need, the interface for a Windows Service contains a special event called OnCustomCommand. The event arguments include a numeric code that can serve as a command sent to the Windows Service. The code can be any number in the range 128 to 255. (The numbers under 128 are reserved for use by the operating system.)

To fire the event and send a custom command to a service, the ExecuteCommand method of the ServiceController is used. The ExecuteCommand method takes the numeric code that needs to be sent to the service as a parameter. When this method is accessed, the ServiceController class tells the Service Control Manager to fire the OnCustomCommand event in the service, and to pass it the numeric code.

The next example demonstrates this process in action. Suppose you want to be able to change the interval between beeps for the CounterMonitor service. You cannot directly send the beep interval that you want, but you can pick various values of the interval, and associate a custom command numeric code with each.

Suppose you want to be able to set intervals of 1 second, 3 seconds (the default), or 10 seconds. You could set up the following correspondence:

Open table as spreadsheet

Custom Command Numeric Code

Beep Interval

201

One second (1,000 milliseconds)

203

Three seconds (3,000 milliseconds)

210

Ten seconds (10,000 milliseconds)

Those correspondences in the table are completely arbitrary. You could use any codes between 128 and 255 to associate with the beep intervals. These were chosen because they are easy to remember.

First, you need to change the CounterMonitor service so that it is able to accept the custom commands for the beep interval. To do that, first make sure the CounterMonitor service is uninstalled from any previous installs. Then open the Visual Studio 2005 project for the CounterMonitor service.

Create an OnCustomCommand event in the service: Open the code window for CounterMonitor.vb and type Protected Overrides OnCustomCommand. By this point, IntelliSense will kick in and you can press the Tab key to autocomplete the shell event. Notice how it only accepts a single Integer as a parameter:

  Protected Overrides Sub OnCustomCommand(ByVal command As Integer) End Sub 

In the OnCustomCommand event, place the following code:

  Timer1.Enabled = False Select Case command     Case 201         Timer1.Interval = 1000     Case 203         Timer1.Interval = 3000     Case 210         Timer1.Interval = 10000 End Select Timer1.Enabled = True 

Build the countermonitor service, reinstall it, and start it.

Now you can enhance the CounterTest application created earlier to set the interval. To allow the user to pick the interval, you will use radio buttons. On the CounterTest program Form1 (which currently contains five buttons), place three radio buttons. Set their text labels as follows:

 RadioButton1 - 1 second RadioButton2 - 3 seconds RadioButton3 - 10 seconds 

Place a button directly under these option buttons. Name it btnSetInterval and set its text to Set Interval. In the click event for this button, place the following code:

  Dim nIntervalCommand As Integer = 203 If RadioButton1.Checked Then     nIntervalCommand = 201 End If If RadioButton2.Checked Then     nIntervalCommand = 203 End If If RadioButton3.Checked Then     nIntervalCommand = 210 End If myController.ExecuteCommand(nIntervalCommand) 

At this point, Form1 should look something like the screen shown in Figure 31-8.

image from book
Figure 31-8

Start the CounterTest control program and test the capability to change the beep interval. Make sure the performance counter is high enough that the CounterMonitor service beeps, and remember that every time you stop and restart the service, it resets the beep interval to three seconds.




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