VB.NET  •  Start a Service Using the Thread's Start Method

Listing 3. The Start method calls the procedure indicated as a parameter of the Thread's constructor. You start the service by showing the main frmFSVC form, and stop the service by setting the form to Nothing.

Public Class VSMFileService
   Inherits System.ServiceProcess.ServiceBase
   Private t As Thread
   Private ui As New ServiceUI()

   Protected Overrides Sub OnStart( _
      ByVal args() As String)
      t = New Thread(AddressOf ui.StartUI)
      t.Start()
   End Sub

   Protected Overrides Sub OnStop()
      ui.StopUI()
      t.Interrupt()
      t.Join()
      t = Nothing
   End Sub
End Class

Public Class ServiceUI
   Private frm As New frmFSVC()
   Public Sub StartUI()
      frm.ShowDialog()
   End Sub
   Public Sub StopUI()
      frm.Show()
      frm.Hide()
      frm = Nothing
   End Sub
End Class