form1里有两个command,两个text。command1里的代码是这样的:
Private Sub Command1_Click()
Dim i As Single
For i = 1 To 30000
Text1.Refresh
Text1.Text = i
Next i
End Sub
command2里的代码是这样的:
Private Sub Command2_Click()
Dim j As Single
For j = 1 To 30000
Text2.Refresh
Text2.Text = j
Next j
End Sub
我的目的就是点击command1后,再点击command2,使它们能同时运行。我这样是想锻炼一下多线程的用法,请高手指导一下这样的程序应该怎么写才能实现上述功能。
Private Sub Command1_Click()
Dim i As Single
For i = 1 To 30000
Text1.Refresh
Text1.Text = i
Next i
End Sub
command2里的代码是这样的:
Private Sub Command2_Click()
Dim j As Single
For j = 1 To 30000
Text2.Refresh
Text2.Text = j
Next j
End Sub
我的目的就是点击command1后,再点击command2,使它们能同时运行。我这样是想锻炼一下多线程的用法,请高手指导一下这样的程序应该怎么写才能实现上述功能。
创建多线程,首先代码要放在模块里面(注意备份,可能正在运行VB就死掉了)
Public Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long '修改过的申明然后找个地方
CreateThread(Nul,ByVal 0&,AddressOf ***你的函数/过程名称***,ByVal 0,0,**你用来保存线程号的变量***)
============
Dim blCMD1, blCMD2 As BooleanPrivate Sub Command1_Click()
blCMD1 = True
abcdef
blCMD1 = False
End SubPrivate Sub Command2_Click()
blCMD2 = True
End SubSub abcdef()
Dim i As Single
Dim j As Single
Do
If i = 30000 And blCMD2 Then GoTo asdfasdf
Text1.Text = i
DoEvents
i = i + 1
If blCMD2 = False Then GoTo qwertyu
asdfasdf:
Text2.Text = j
j = j + 1
If j = 30000 Then Exit Do
qwertyu:
Loop
blCMD2 = False
End Sub
Private Sub Command1_Click()
blCMD1 = True
abcdef
End Sub
Private Sub Command2_Click()
blCMD2 = True
abcdef
End Sub
Sub abcdef()
Dim i, j As Single
Do
If Not blCMD1 Then GoTo asdfasdf
Text1.Text = i
DoEvents
i = i + 1
If i = 30000 Then blCMD1 = False
If blCMD2 = False Then GoTo qwertyu
asdfasdf:
Text2.Text = j
DoEvents
j = j + 1
If j = 30000 Then blCMD2 = False
qwertyu:
If Not blCMD1 And Not blCMD2 Then Exit Do
Loop
End Sub
Dim blCMD1, blCMD2, BOOLabcdef As Boolean
Private Sub Command1_Click()
blCMD1 = True
If Not BOOLabcdef Then abcdef
End Sub
Private Sub Command2_Click()
blCMD2 = True
If Not BOOLabcdef Then abcdef
End Sub
Sub abcdef()
Dim i, j As Single
BOOLabcdef = True
Do
If Not blCMD1 Then GoTo asdfasdf
Text1.Text = i
DoEvents
i = i + 1
If i = 30000 Then blCMD1 = False
If blCMD2 = False Then GoTo qwertyu
asdfasdf:
Text2.Text = j
DoEvents
j = j + 1
If j = 30000 Then blCMD2 = False
qwertyu:
If Not blCMD1 And Not blCMD2 Then Exit Do
Loop
BOOLabcdef = False
End Sub