'模块: 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 TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long Private mThreadHandle As Long '示例 Function addnumber() As Long Dim i As Long For i = 0 To 100000 Form1.Label1.Caption = i Next End Function '创建线程 Public Function CreateNewThread(ByVal cFunction As Long) As Long Dim mhandle As Long Dim lpThreadID As Long mhandle = CreateThread(ByVal 0&, ByVal 0&, cFunction, ByVal 0&, 0, lpThreadID) If mhandle <> 0 Then mThreadHandle = mhandle End Function '终止线程 Public Function TerminateTh() As Long SuspendThread mThreadHandle TerminateThread mThreadHandle, ByVal 0& TerminateThread lpThreadID, ByVal 0& End Function '---------------------------------邪恶的分割线------------------------------------ '下面是窗体代码: Private Sub Command1_Click() '为addnumber过程创建线程 Call CreateNewThread(AddressOf addnumber) '//这里就是创建线程 调用事件 这里的事件addnumber就是模块中的那个过程 End Sub Private Sub Command2_Click() TerminateTh '终止线程 Unload Me '结束程序 End Sub
VB能实现多线程,感觉不是特别好用。
代码非原创,收集来自网络。