由线程调用模块,怎么控制主窗体的进度条?
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim thread1 As Threading.Thread
thread1 = New Threading.Thread(AddressOf program1)
thread1.Start()
End Sub
End Class
''''''''''''''''''''''下面在模块里面
Module Module1
Public Sub program1()
Form1.ProgressBar1.Value = 50
End Sub
End Module
'''''''''''''''''''''''''''''''
不报错,进度条也没反应。。。求助。。。。。 多线程 模块 进度条
[解决办法]
好吧,新建一个项目,拉上两个ProgressBar,一个Button
窗体代码
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'a.i = Not a.i '暂停或继续的开关
Class1.i = Not Class1.i
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Show()
Me.ProgressBar1.Value = 0
Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Maximum = 10000
Me.ProgressBar2.Maximum = 10000
Dim a As New Class1(Me)
For t As Integer = 0 To 10000 '感受线程
Application.DoEvents()
ProgressBar2.Value = t
Next
End Sub
End Class
Public Class Class1
Dim a As Form = Nothing
Dim Thread1 As New System.Threading.Thread(AddressOf abc)
Public Shared i As Boolean = True, j As Integer = 0
Public Delegate Sub DelegateSub(ByVal text As String)
Public Sub New(ByVal TheForm As Form) '注意修改后NEW
a = TheForm
Thread1.Start()
End Sub
Private Sub SetText(ByVal value As String)
Form1.ProgressBar1.Value = value
End Sub
Public Sub abc()
Do
If i Then
j += 1
If j = 10000 Then j = 0 'j=0是为了演试循环,可以Exit sub的
a.Invoke(New DelegateSub(AddressOf SetText), j.ToString)
Application.DoEvents()
End If
Loop
Thread1.Abort() '如果不需要演试循环功能,可以在上面的DO中在达到最大值时提交
End Sub
End Class