Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

Build Your First .NET Windows Service (Continued)

The ServicesToRun object contains an array of service classes that you should run under the same process space (thereby conserving system resources), but can still be managed independently as services. This service array is passed to the ServiceBase.Run() method, which looks at the services and starts up any service whose Startup property is set to Automatic.

Beware that when creating your first service, the service class is named Service1 automatically. Even if you rename the service class, you still must change the line of code manually from this:

ServicesToRun = New _
   System.ServiceProcess.ServiceBase() _
   {New Service1}
   System.ServiceProcess.ServiceBase.Run( _
      ServicesToRun)

to this:

ServicesToRun = New _
   System.ServiceProcess.ServiceBase() _
   {New AppMonitorService}
   System.ServiceProcess.ServiceBase.Run( _
      ServicesToRun)
ADVERTISEMENT

You can hook into several Windows Service events by overriding the base class routines for these events—there's no way in VS.NET 2003 to auto-generate these overridden events (see Listing 1 to view the code for the overridden service events in the AppMonitor service). Because the procedures are overridden, they need to match the procedure names and arguments exactly in order to compile correctly.

In the example, you respond to all of the available Windows Service events by defining overriding procedures for all possible events. In addition to start, stop, pause, and continue events, notice that there's also a custom command procedure:

Protected Overrides Sub OnCustomCommand( _
   ByVal iCommand As Integer)

This procedure allows an external application to send an integer command to the service while it's running. This procedure cannot return a value, because its intended use was for primitive one-way IPC communications between a process and a service. In the AppMonitor service example, you can use OnCustomCommand to provide a way for applications to send monitoring events to the AppMonitor service. From there, merely log the command to a test log file to keep things simple. You could easily expand this for use in a production environment—for example, you could store monitors in a database or send the messages to a message queue.

You can also send parameters to a Windows Service on startup. Notice that the OnStart routine takes an array of strings as an argument:

Protected Overrides Sub OnStart( _
   ByVal args() As String)

Use a parameter in the AppMonitor example to specify the name and location of the log file. If the service is started without an argument, simply use a file in the root directory for logging. To send a parameter to a service, use the Services MMC snap-in, select properties of the service, and enter the parameter in the Start Parameters textbox (see Figure 2).

Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home