title: Threading progress bar in vb.net - thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on
body: <p>Would anyone be able to help me here please.  I'm fairly new to VB.net and threading so im just trying to figure out what is happening.</p>
  
  <p>When I debug this I am getting the error</p>
  
  <blockquote>
    <p>thread operation not valid: Control 'ProgressBar1' accessed from a
    thread other than the thread it was created on.</p>
  </blockquote>
  
  <p>I'm a little lost as to why the error is occuring or how to fix it.  I've had to put the progress bar in a separate thread otherwise the GUI crashes</p>
  
  <pre><code>Private Sub InstallTheAgent_Click(ByVal sender As System.Object, ByVal e As     System.EventArgs) Handles InstallTheAgent.Click
      MsgBox("Click OK to continue to install the Agent")
      Dim t As Thread
  
      t = New Threading.Thread(AddressOf WorkInstallAgent)
      t.Start()
  End Sub
  
  Private Sub WorkInstallAgent()
      ProgressBarThread()
      Dim DeviceLines As String() = IO.File.ReadAllLines(DynPath &amp; "\machines.txt")
      ProgressBar1.Minimum = 0
      ProgressBar1.Maximum = DeviceLines.Length
  
      Using myReader As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(DynPath &amp; "\machines.txt")
          ProgressBarThread()
          Do While myReader.Peek() &gt;= 0
  
              Dim computerName As String = myReader.ReadLine
              If My.Computer.Network.Ping(computerName) = True Then
                  Call CopyAgent(computerName)
                  Call InstallAgent(computerName)
                  Call Agent(computerName)
                  Call RemoveTempSoftware(computerName)
              End If
              ProgressBar1.Value = ProgressBar1.Value + 1
          Loop
      End Using
  
      MsgBox("Installation of the Agent Complete")
      ProgressBar1.Value = 0
  
  
  End Sub
  
  Public Sub ProgressBar()
  
      If Me.InvokeRequired Then
          Me.Invoke(New MethodInvoker(AddressOf ProgressBar))
      Else
          Dim DeviceLines As String() = IO.File.ReadAllLines(DynPath &amp; "\machines.txt")
          ProgressBar1.Minimum = 0
  
          ProgressBar1.Maximum = DeviceLines.Length
      End If
  End Sub
  
  Public Sub ProgressBarThread()
      Dim p As Thread
  
      p = New Threading.Thread(AddressOf ProgressBar)
      p.Start()
  
  End Sub
  </code></pre>