Communicating with the Service

Up to this point, you have learned how to do the following:

  • Create a Windows service using Visual Basic
  • Start and stop a service with the Service Control Manager from the Control Panel
  • Make a service work with a system-level function such as a FileSystemWatcher

If these procedures are sufficient to start, stop, and check on the service through the Server Explorer or the Service Control Manager, and there is no need for any other communication with the service, then this is all you have to do. However, it is often helpful to create a specialized application to manipulate your service. This application will typically be able to start and stop a service, and check on its status. The application may also need to communicate with the service to change its configuration. Such an application is often referred to as a control panel for the service, even though it does not necessarily reside in the operating system's Control Panel. A commonly used example of such an application is the SQL Server Service Manager, whose icon appears in the tray on the taskbar (normally in the lower-right section of the screen) if you have SQL Server installed.

Such an application needs a way to communicate with the service. The .NET Framework base class that is used for such communication is ServiceController. It is in the System.ServiceProcess namespace. You need to add a reference to System.ServiceProcess.dll (which contains this namespace) before a project can use the ...

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.