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. The thread is removed from the Threads collection if it has stopped already. 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