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 ...
Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.