Custom Commands

Some services need additional operations besides starting and stopping. For example, for the FileWatcherService, you might want to support multiple file extensions, using a different FileSystemWatcher component for each.

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.

You can modify the example code to demonstrate this process in action. Suppose you want to be able to change the file filter being used for the FileWatcherService service. You cannot directly send the filter that you want, but you can pick various values of the filter, and associate a custom command numeric code with each.

For example, assume you want to be able to set filters of *.txt, *.dat, or *.docx. You could set up the following correspondence:

Custom Command Numeric Code Filter for FileSystemWatcher
201 *.txt
203 *.docx
210 *.dat

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

First, you need to change the FileWatcherService service so that it is able to accept the custom commands for the beep interval. To do that, first make sure the FileWatcherService service is uninstalled from any previous installs.

Create an OnCustomCommand event in the service: Open the code window for FileWatcherService.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)
    MyBase.OnCustomCommand(command)
End Sub

In the OnCustomCommand event handler, replace the single line that was generated automatically (the one beginning with MyBase) with the following code:

     Select Case command
         Case 201
             FileSystemWatcher1.Filter = "*.txt"
         Case 203
             FileSystemWatcher1.Filter = "*.docx"
         Case 210
             FileSystemWatcher1.Filter = "*.dat"
     End Select

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

Now you can enhance the FileWatcherPanel application created earlier to set the filter. To enable users to select the file filter, you will use a ComboBox. Add a Label to your window with the text “Select File Extension.” Next add a ComboBox control below it. Name it “ComboBoxFileType” then go to the Items property of the ComboBox and use the ellipsis button to add new items. Add three new ListBoxItems to the collection. Set the contents of each using TXT, DOCX, and DAT, respectively. Once you have defined the three items you should set the SelectedIndex property of your ComboBox to 0.

Place a button directly under your ComboBox. Name it ButtonFilter and set its text to Set Filter, as follows:.

Private Sub ButtonFilter_Click(sender As Object, 
            e As RoutedEventArgs) Handles ButtonFilter.Click
    Dim s As String = ComboBoxFileType.Text
    Select Case s
        Case "TXT"
            myController.ExecuteCommand(201)
        Case "DOCX"
            myController.ExecuteCommand(203)
        Case Else
            myController.ExecuteCommand(210)
    End Select
End Sub

At this point, the MainWindow should look something like the screen shown in Figure 16.4.

Figure 16.4 File Watcher Control Panel

16.4

Start the FileWatcherPanel control program and test the capability to change the filter by adding different file types with each filter setting and examining the resulting logged events.

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

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