'加入一个类模块名为clsThreads.cls,添加以下代码
Private Declare Function CreateThread Lib "kernel32" (ByVal 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 ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Private Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Thread As Long
Private ThreadStatus As Boolean
Public Sub Initialize(lpfnBasFunc As Long)
Dim dwStackSize As Long
Dim dwCreationFlags As Long
Dim lpThreadId As Long
Dim lpParameter As Long
Dim myNull As Long
myNull = 0& 'create a null pointer
dwStackSize = 0 '0表示用exe stack size
dwCreationFlags = 4 '用4表示初始化后先不激活,让别人来激活
Thread = CreateThread(myNull, dwStackSize, lpfnBasFunc, myNull, dwCreationFlags, lpThreadId)
If Thread = myNull Then
MsgBox "create thread failed"
Else
Debug.Print Thread
Debug.Print GetCurrentThread
Debug.Print GetThreadPriority(Thread)
Debug.Print GetThreadPriority(GetCurrentThread)
Debug.Print SetThreadPriority(GetCurrentThread, 15&)
End If
End Sub
Public Property Let Enabled(ByVal vNewValue As Boolean)
If vNewValue = True And ThreadStatus = False Then
ResumeThread Thread
ThreadStatus = True
ElseIf ThreadStatus = True Then
SuspendThread Thread
ThreadStatus = False
End If
End Property
Public Sub Terminate()
On Error Resume Next
Dim exitcode As Long
Dim ret As Long
ret = GetExitCodeThread(Thread, exitcode)
Debug.Print ret, exitcode, Thread
ret = TerminateThread(Thread, exitcode)
Debug.Print ret
ret = CloseHandle(Thread)
Debug.Print ret
End Sub
'加入一个模块Module1.bas,添加以下代码
Public CR As Boolean
Public Sub RunRunRun()
Form1.Label1.Caption = "没有Doevents的无限循环已经在新开的线程中开始运行了!"
Do While CR
Form1.Label3.Caption = "-"
Form1.Label3.Refresh
Form1.Label3.Caption = "*"
Form1.Label3.Refresh
Loop
End Sub
'form1中需要两个command
Option Explicit
Dim Run3Thread As New clsThreads
Private Sub Command1_Click()
If Command1.Caption = "新开线程" Then
Command1.Caption = "终止线程"
CR = True
Run3Thread.Initialize AddressOf RunRunRun
Run3Thread.Enabled = True
Command2.Enabled = True
Else
CR = False
'Run3Thread.Terminate
Command2.Enabled = False
Command1.Caption = "新开线程"
Set Run3Thread = Nothing
End If
End Sub
Private Sub Command2_Click()
If Command2.Caption = "暂停线程" Then
Command2.Caption = "继续线程"
Run3Thread.Enabled = False
Else
Command2.Caption = "暂停线程"
Run3Thread.Enabled = True
End If
End Sub
Private Sub Form_Load()
Command1.Caption = "新开线程"
Command2.Caption = "暂停线程"
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next