模块里面的代码:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Public Sub Fill1()
Dim i As Integer
For i = 0 To 255
Form1.Picture1.BackColor = RGB(i, 0, 0)
Form1.Label1.Caption = CStr(i)
Sleep 100
Next i
End SubPublic Sub Fill2()
Dim i As Integer
For i = 0 To 255
Form1.Picture2.BackColor = RGB(0, i, 0)
Form1.Label2.Caption = CStr(i)
Sleep 100
Next i
End SubForm里面的代码:Private Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Dim id1, id2 As Long
Private Sub Command1_Click()id1 = CreateThread(0, 0, AddressOf Module1.Fill1, 0, 0, lpThreadId1)id2 = CreateThread(0, 0, AddressOf Module1.Fill2, 0, 0, lpThreadId2)End Sub
Private Sub Form_Unload(Cancel As Integer)
TerminateThread id1, 0
TerminateThread id2, 0
Unload Me
End Sub为什么运行后form1里面的两个picture控件不能改变颜色,但是i的值是变化的。
如果不用进程的话,picture的颜色却可以改变。
求解释
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Public Sub Fill1()
Dim i As Integer
For i = 0 To 255
Form1.Picture1.BackColor = RGB(i, 0, 0)
Form1.Label1.Caption = CStr(i)
Sleep 100
Next i
End SubPublic Sub Fill2()
Dim i As Integer
For i = 0 To 255
Form1.Picture2.BackColor = RGB(0, i, 0)
Form1.Label2.Caption = CStr(i)
Sleep 100
Next i
End SubForm里面的代码:Private Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Dim id1, id2 As Long
Private Sub Command1_Click()id1 = CreateThread(0, 0, AddressOf Module1.Fill1, 0, 0, lpThreadId1)id2 = CreateThread(0, 0, AddressOf Module1.Fill2, 0, 0, lpThreadId2)End Sub
Private Sub Form_Unload(Cancel As Integer)
TerminateThread id1, 0
TerminateThread id2, 0
Unload Me
End Sub为什么运行后form1里面的两个picture控件不能改变颜色,但是i的值是变化的。
如果不用进程的话,picture的颜色却可以改变。
求解释
目前为止,我还没有看到一个用VB多线程来开发的成功软件。如果你实在要使用多线程,那你可以考虑购买ThreadFactory来实现。稳定性能保证。