關於多線程進度條的問題
以下是我的源代碼
Imports System.Threading
Public Class Form1
Private Delegate Sub InvokeDelegate()
Dim th1 As Thread
Dim th2 As Thread
Private Sub Delegate1()
BeginInvoke(New InvokeDelegate(AddressOf thread1))
End Sub
Private Sub Delegate2()
Dim myInvoker As MethodInvoker
myInvoker = New MethodInvoker(AddressOf thread2)
BeginInvoke(myInvoker)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
th1 = New Thread(AddressOf Delegate1)
th2 = New Thread(AddressOf Delegate2)
th1.Start()
th2.Start()
End Sub
Private Sub thread1()
Dim i As Integer
Do While True
ProgressBar1.Value = i
i += 1
If i = 100 Then i = 0
Thread.Sleep(200)
Loop
End Sub
Private Sub thread2()
Dim ii As Integer
Do While True
ProgressBar2.Value = ii
ii += 1
If ii = 100 Then ii = 0
Thread.Sleep(250)
Loop
End Sub
End Class
執行不會出錯
可是只有一個進度條會走動
這是怎麼回事呀?
要如何改進呢??
[解决办法]
你的代码有问题,这样做始终在一个 Do While True出不来
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
最新版本:20070212
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
[解决办法]
Private Delegate Sub InvokeDelegate(ByVal i As Integer)
Dim th1 As Thread
Dim th2 As Thread
Private Sub Delegate1()
Dim i As Integer
Do While True
BeginInvoke(New InvokeDelegate(AddressOf thread1), i)
i += 1
If i = 100 Then i = 0
Thread.Sleep(200)
Loop
End Sub
Private Sub Delegate2()
Dim ii As Integer
Do While True
BeginInvoke(New InvokeDelegate(AddressOf thread2), ii)
ii += 1
If ii = 100 Then ii = 0
Thread.Sleep(250)
Loop
End Sub
Private Sub thread1(ByVal i As Integer)
ProgressBar1.Value = i
End Sub
Private Sub thread2(ByVal i As Integer)
ProgressBar2.Value = i
End Sub