但是用vb做多线程的关键不在于创建,而是在于线程的不稳定,其不稳定是由于vb6自身的控件(如按钮、文本框等)不是线程安全的,怎么用你程序就可能怎么挂,所以这些个玩样儿在多线程中统统不能用。
那么UI的实现也得用API实现。这样vb的多线程才相对要稳定。但是vb的可视化开发的优势又荡然无存。
窗体代码:Private Sub Command1_Click()'为addnumber过程创建线程
Call CreateNewThread(AddressOf addnumber)
End Sub
Private Sub Command2_Click()
TerminateTh '终止线程
Unload Me'结束程序
End Sub
模块: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 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 ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As LongPrivate Const MAXLONG = &H7FFFFFFF
Private Const THREAD_BASE_PRIORITY_IDLE = -15
Private Const THREAD_BASE_PRIORITY_LOWRT = 15
Private Const THREAD_BASE_PRIORITY_MAX = 2
Private Const THREAD_BASE_PRIORITY_MIN = -2
Private Const THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAX
Private Const THREAD_PRIORITY_LOWEST = THREAD_BASE_PRIORITY_MIN
Private Const THREAD_PRIORITY_ABOVE_NORMAL = (THREAD_PRIORITY_HIGHEST - 1)
Private Const THREAD_PRIORITY_BELOW_NORMAL = (THREAD_PRIORITY_LOWEST + 1)
Private Const THREAD_PRIORITY_ERROR_RETURN = (MAXLONG)
Private Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLE
Private Const THREAD_PRIORITY_NORMAL = 0
Private Const THREAD_PRIORITY_TIME_CRITICAL = THREAD_BASE_PRIORITY_LOWRT
Private Const CREATE_ALWAYS = 2
Private Const CREATE_NEW = 1
Private Const CREATE_NEW_CONSOLE = &H10
Private Const CREATE_NEW_PROCESS_GROUP = &H200
Private Const CREATE_NO_WINDOW = &H8000000
Private Const CREATE_PROCESS_DEBUG_EVENT = 3
Private Const CREATE_SUSPENDED = &H4
Private Const CREATE_THREAD_DEBUG_EVENT = 2Public Enum ThreadPriority
tpLowest = THREAD_PRIORITY_LOWEST
tpBelowNormal = THREAD_PRIORITY_BELOW_NORMAL
tpNormal = THREAD_PRIORITY_NORMAL
tpAboveNormal = THREAD_PRIORITY_ABOVE_NORMAL
tpHighest = THREAD_PRIORITY_HIGHEST
End EnumPrivate 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
先说多线程:方法 1 : CreateThread
不用多说,万变不离其宗,这是 Win32 内创建线程的唯一系统方法,以下介绍的多线程创建方式虽然不直接接触这个 Win32 APi,但系统也是通过他创建的。
方法 2 : TimerSetEvent
这个函数是 Win32 系统的一个高精度计时器函数,通过调用这个函数,系统会创建一个新的线程,让Timer回调函数中的代码运行在这个新线程中。
方法 3 : SHCreateThread
很多人应该没见过这个函数,这是 shlwapi.dll 的一个导出函数,这个函数看字面也能理解是个创建多线程的函数,而且。。。 他是基于对象 *** 作的多线程函数,也就是说他能够稳定的运行一些用上面2种方法创建,但不能稳定运行的代码。至少这个函数在 IDE 的环境下表现的中规中矩! 使用方法可查 API-GUIDE
方法 4 :CreateRemoteThread
创建远程线程,经测试一点也不稳定,根本没法说下去了,还不如直接调用 CreateThread
但,做为一种线程的创建方法,还是希望能够熟悉一下,如果不用VB写代码,要写跨进程线程,这是一个简单易行的办法,否则只能继续SetWindowHook了,熟悉汇编指令的还可以远程分配内存写硬编码执行
方法 5 : 简单说下 ActiveX 方式的多线程
用 ActiveX 创建线程与其他方式创建的线程最重要的地方要主义,任何VB函数的调用最好都建立在新线程中新初始化的对象基础上执行,不要跨对象调用,而 ActiveX 的安全线程本身也正是利用了这点,不知道研究 ActiveX 的老大们有没注意,利用他特性实现的多线程,是初始化的一个新的线程,而这个线程中初始化的对象则是整个工程。很大的资源浪费,而且有时候还不好控制,不是你想要的效果。总之一句话,新线程中要自己初始化新的对象而且最好不要用实例继承
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)