RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Write Your Own Windows Services : Page 5

If you're using Windows, you're no stranger to Windows services, but do you know how to write one of your own? In this article, you'll learn not only how to build your own Windows service, but how to build a client with which to control it.

Building a Client to Control the Windows Service
The user interface of this application is similar to the Service Manager found in SQL Server 2000. It allows you to start/continue, pause, and stop your Windows service. In addition, I will also show you how you can minimize the Windows application so that it appears in the System Tray.

Add a new Windows application project to the current solution and name it as WordFiles. Add the following controls to the default Form1:

  • StatusStrip
  • ServiceController
  • NotifyIcon
  • ContextMenuStrip
Figure 5. Populating: The Form1 with the various controls.

Populate Form1 with the following controls (see also Figure 5):

  • TextBox
  • Button
Add the menu items inside the ContextMenuStrip control as shown in Figure 5 (you need to first select ContextMenuStrip1 so that you can create the menu items).

Set the properties for Form1 as follows:









For NotifyIcon1, set its ContextMenuStrip property to ContextMenuStrip1 and assign it an icon (see Figure 6). Also set its Text property to "Word Files Monitoring App".

Author's Note: I suggest you download the source code for this article so that you need not go through all the steps in building the UI.

You can now switch to the code-behind of Form1 to start coding.

Figure 6. Assign an Icon: Configuring NotifyIcon1.

First, import the following namespace:

Imports System.IO
Next, define the StartService() subroutine to start (or continue) the Windows service (see Listing 2).

Observe that the arguments to pass to the Windows service is a string array (args).

Next, define the PauseService() subroutine to pause the Windows service:

    Private Sub PauseService()
        If ServiceController1.CanPauseAndContinue Then
            ToolStripStatusLabel1.Text = _
            ToolStripStatusLabel1.Text = "Service cannot be paused."
        End If
    End Sub
Define the StopService() subroutine to stop the Windows service:

    Private Sub StopService()
        If ServiceController1.Status = _
           ServiceProcess.ServiceControllerStatus.Stopped Then
            ToolStripStatusLabel1.Text = "Service is not running."
            Exit Sub
        End If

        If ServiceController1.Status = _
           ServiceProcess.ServiceControllerStatus.StopPending Then
            ToolStripStatusLabel1.Text = "Service is stopping..."
            Exit Sub
        End If

            ToolStripStatusLabel1.Text = _
        Catch ex As Exception
        End Try
    End Sub
Define the RefreshService() subroutine to refresh the status of the Windows service:

    Private Sub RefreshService()
        ToolStripStatusLabel1.Text = "Refreshing service..."
        With ServiceController1
            .MachineName = txtServer.Text
        End With
        ToolStripStatusLabel1.Text = _
    End Sub
You can now wire up all the event handlers for the various buttons with the subroutines just defined (Listing 3).

When the form is loaded for the first time, set the ServiceController1 control to the Windows service that you are controlling:

    Private Sub Form1_Load( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MyBase.Load
        '---display the local computer name---
        txtServer.Text = My.Computer.Name
        With ServiceController1
            .ServiceName = "WordFilesMonitor"
            .MachineName = txtServer.Text
        End With
    End Sub
Now, when the form is minimized, you want it to appear in the System Tray as an icon. In addition, you do not want it to appear in the task bar. To do this, define the MinimizeWindows() subroutine as follows:

    Private Sub MinimizeWindow()
        With Me
            '---minimize the window---
            .WindowState = FormWindowState.Minimized
            '---do not show application in task bar---
            .ShowInTaskbar = False
        End With
    End Sub
The RestoreWindow() subroutine does the exact opposite—restore the form and show it in the task bar:

    Private Sub RestoreWindow()
        With Me
            '---restore the window---
            .WindowState = FormWindowState.Normal
            '---show application in task bar---
            .ShowInTaskbar = True
        End With
    End Sub
When the form is minimized, the user can double-click on the icon to restore the window (see Figure 7).

Figure 7. Restore the Window: The NotifyIcon in the System Tray.
Figure 8. Minimize the App: Clicking on the close button will minimize the application to System Tray.

This is accomplished by the servicing the MouseDoubleClick event handler of the NotifyIcon control:

    '---Double-click on the icon in System Tray---
    Private Sub NotifyIcon1_MouseDoubleClick( _
       ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.MouseEventArgs) _
       Handles NotifyIcon1.MouseDoubleClick
    End Sub
Finally, if the user clicks on the close button of the form (see Figure 8), it will minimize the application to the System Tray instead of ending the application.

This is accomplished by the FormClosing event handler of Form1:

    Private Sub Form1_FormClosing( _
       ByVal sender As Object, _
       ByVal e As System.Windows.Forms.FormClosingEventArgs) _
       Handles Me.FormClosing
        e.Cancel = True
    End Sub
Note that you need to set the e.Cancel property to True in order to prevent the application from ending.

That's it! You are now ready to test the application.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date