请帮我看一下这个多线程的代码有没有逻辑上的错误
本帖最后由 ytldhd 于 2013-02-20 14:22:36 编辑 我用的是VS2012中的VB。
Public Class Form1
Public myThread(5) As Threading.Thread
Public myProgressbarArray(5) As ProgressBar
Public myThreadSwitch(5) As Boolean
Public ThreadLock As New Object
Public CompletedThreadCount As Integer
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) _
Handles Me.FormClosed
For index = 0 To 4
myThreadSwitch(index) = False
myThread(index).Resume()
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
myProgressbarArray(0) = ProgressBar1
myProgressbarArray(1) = ProgressBar2
myProgressbarArray(2) = ProgressBar3
myProgressbarArray(3) = ProgressBar4
myProgressbarArray(4) = ProgressBar5
CompletedThreadCount = 0
For index = 0 To 4
myThreadSwitch(index) = True
myProgressbarArray(index).Maximum = 1000
Next
For index = 0 To 4
myThread(index) = New Threading.Thread(AddressOf MyThreadWork)
Next
For index = 0 To 4
myThread(index).Start(index)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Enabled = False
For index = 0 To 4
myProgressbarArray(index).Value = 0
Next
For index = 0 To 4
myThread(index).Resume()
Next
End Sub
Private Sub ThreadArrayWatcher(ThreadIndex As Integer)
SyncLock ThreadLock
CompletedThreadCount += 1
If CompletedThreadCount >= 5 Then
CompletedThreadCount = 0
Button1.Enabled = True
End If
End SyncLock
End Sub
Private Sub myThreadWork(ByVal ThreadIndex As Object)
Do
myThread(ThreadIndex).Suspend()
If myThreadSwitch(ThreadIndex) = False Then
Exit Do
End If
For indxe = 0 To myProgressbarArray(ThreadIndex).Maximum - 1
myProgressbarArray(ThreadIndex).Value += 1
Next
ThreadArrayWatcher(ThreadIndex)
Loop
End Sub
End Class
myProgressbarArray(0) = ProgressBar1
myProgressbarArray(1) = ProgressBar2
myProgressbarArray(2) = ProgressBar3
myProgressbarArray(3) = ProgressBar4
myProgressbarArray(4) = ProgressBar5
Public myProgressbarArray() As ProgressBar
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Control.CheckForIllegalCrossThreadCalls = False
myProgressbarArray={ProgressBar1,ProgressBar2,ProgressBar3,ProgressBar4,ProgressBar5}
...
End Sub