title: VB.Net Multiple background workers - Only last task completes
body: <p>I have been pulling my hair out trying to get this to work. If I step through the code in debugger it all works great. </p>
  
  <p>My problem is if I just run it, only the last task responds. I'm guessing I am overwriting the background working or something. I am sure I am doing a few things wrong but my code is now messy as I tried many way while searching. I know of the threadpool and .Net 4.0 tasks but having a hard time getting to do what I need.</p>
  
  <p>Basicly I am writing a program (trying more likely) that takes a list of computers and pings then, then checks their uptime and reports back.</p>
  
  <p>This works fine in the UI thread (Obviously that locks up my screen). I can have the background worker just do this, but then it does each computer 1 by one, and while the screen is responsive it still takes a long time.</p>
  
  <p>So my answer was to have a for loop for each server launching a new background worker thread. My solution does not work.</p>
  
  <p>I have seen other threads that I could do it, but I need to use with events to call code to update to UI when each is done.</p>
  
  <p>What is the most simple way to do this?</p>
  
  <p>Here is my code. Most is just copy paste + modify till I get it working right.</p>
  
  <p>So In the main class I have the testworker.</p>
  
  <p>(I tried using Testworker() but it said I could not do that WithEvents)</p>
  
  <p>When I click the button the list loads.</p>
  
  <pre><code>Private WithEvents TestWorker As System.ComponentModel.BackgroundWorker
  
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
      Button1.IsEnabled = False
  
      Dim indexMax As Integer
                      indexMax = DataGridStatus.Items.Count
      For index = 1 To (indexMax)
          Dim Temp As ServerInfo = DataGridStatus.Items(index - 1)
          Temp.Index = index - 1
          Call_Thread(Temp)
      Next
  End Sub
  
  
  Private Sub Call_Thread(ByVal server As ServerInfo)
      Dim localserver As ServerInfo = server
  
      TestWorker = New System.ComponentModel.BackgroundWorker
      TestWorker.WorkerReportsProgress = True
      TestWorker.WorkerSupportsCancellation = True
      TestWorker.RunWorkerAsync(localserver)
  
  End Sub
  
  Private Sub TestWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles TestWorker.DoWork
  
      Dim iparray As IPHostEntry
      Dim ip() As IPAddress
  
      Dim Server As ServerInfo
      Server = e.Argument
      Try
          'Get IP Address first
          iparray = Dns.GetHostEntry(Server.ServerName)
          ip = iparray.AddressList
          Server.IPAddress = ip(0).ToString
  
          'Try Pinging
          Server.PingResult = PingHost(Server.ServerName)
          If Server.PingResult = "Success" Then
  
              'If ping success, get uptime
              Server.UpTime = GetUptime(Server.ServerName)
          Else
              Server.PingResult = "Failed"
          End If
  
      Catch ex As Exception
          Server.PingResult = "Error"
      End Try
  
      TestWorker.ReportProgress(0, Server)
      Thread.Sleep(1000)
  
  End Sub
  
  
  Private Sub TestWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles TestWorker.ProgressChanged
  
      Dim index As Integer
      Dim serverchange As ServerInfo = DirectCast(e.UserState, ServerInfo)
  
      index = DataGridStatus.Items.IndexOf(serverchange)
      ' index = serverchange.Index
      DataGridStatus.Items.Item(index) = serverchange
  
      ' ProgressBar1.Value = e.ProgressPercentage
      DataGridStatus.Items.Refresh()
  End Sub
  </code></pre>