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.
When I debug this I am getting the error
thread operation not valid: Control ‘ProgressBar1’ accessed from a
thread other than the thread it was created on.
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
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 & "\machines.txt")
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = DeviceLines.Length
Using myReader As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(DynPath & "\machines.txt")
ProgressBarThread()
Do While myReader.Peek() >= 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 & "\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
Progress bar is on GUI thread. If you try to access it form other thread then this error will occur.
Try usin Background worker. Check this link:
http://www.java2s.com/Code/VB/GUI/BackgroundWorkerDemo.htm