任何人都可以在这里帮助我。我对VB.net和线程相当新,所以我只是想弄清楚发生了什么。vb.net线程进度条 - 线程操作无效:从其创建的线程以外的线程访问的控制'ProgressBar1'
当我调试这个我收到错误
线程操作无效:控制“ProgressBar1”从比它创建的线程以外的 线程访问。
我有点失落,为什么错误发生或如何解决它。我必须将进度条放入单独的线程,否则GUI崩溃
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
谢谢,但是我的程序中有大约15个其他选项已经为线程设置,因此切换到后台工作人员是不可行的 – K20GH 2012-02-17 10:31:23