 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: October 2002
Section: Create Windows Services
Author: Gerardo Villeda

VB.NET	Refresh Control Contents
Listing 1	The RefreshThreadStatus procedure refreshes the contents of the ListView control displayed on the screen. The Threads collection contains a reference to all the threads started by your service.  The code in this function loops through the elements in the collection to determine the status of each thread indicated by the thread's ThreadState property. If the thread has already stopped, its removed from the Threads collection. Otherwise, this code adds an item to the ListView control using the AddThreadItem function.  

Public Sub RefreshThreadStatus()
	'Called to refresh the screen
	Dim i As Integer
	Dim item As FileThread
	Me.lvwThreads.Items.Clear()
	For i = Threads.Count To 1 Step -1
		item = Threads.Item(i)
		If item.t.ThreadState = _
			ThreadState.Stopped Then
			item.t = Nothing
			Threads.Remove(i)
		Else
			With item
				AddThreadItem(.tStart, _
					.t.ThreadState,.fn, .ID)
			End With
		End If
	Next
	Me.lvwThreads.Refresh()
	Me.Refresh()
End Sub

VB.NET	Modify Thread Status
Listing 2	A context menu displays when you right-click on an item in the ListView control. These sections of code are executed in response to the click event of the mnuAbort, mnuSuspend, and mnuResume menu objects. The first item in the SelectedIetms property contains the item currently selected. Once the reference is found, the threads Abort, Suspend, and Resume methods are executed to modify the threads status. When required, the code verifies the threads status before any of the methods is executed.

'Abort selected thread
Dim item As ListViewItem
Dim ft As FileThread
If lvwThreads.SelectedItems.Count > 0 Then
	item = lvwThreads.SelectedItems.Item(0)
	ft = Threads.Item(item.SubItems(3).Text)
	ft.t.Abort()
	ft.t.Join()
End If

'Suspend selected thread
Dim item As ListViewItem
Dim ft As FileThread
If lvwThreads.SelectedItems.Count > 0 Then
	item = lvwThreads.SelectedItems.Item(0)
	ft = Threads.Item(item.SubItems(3).Text)
	If ft.t.ThreadState = ThreadState.Running Then
		ft.t.Suspend()
		ft.t.Join()
	End If
End If

'Resume suspended thread
Dim item As ListViewItem
Dim ft As FileThread
If lvwThreads.SelectedItems.Count > 0 Then
	item = lvwThreads.SelectedItems.Item(0)
	ft = Threads.Item(item.SubItems(3).Text)
	If ft.t.ThreadState = ThreadState.Suspended _
		Or ft.t.ThreadState = _
		ThreadState.SuspendRequested Then
		ft.t.Resume()
	End If
End If

VB.NET	Start a Service Using the Threads Start Method 
Listing 3	The Start method calls the procedure indicated as a parameter of the Threads constructor. You start the service by showing the main frmFSVC form. You 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
