private declare function CreateThread Lib "kernel32" (byval pThreadAttributes as any, byval dwStackSize as long, byval lpStartAddress as long, lpParameter as any, byval dwCreationFlags as long, lpThreadID as long) as long. (从vb API text viewer中拿是最真确的语法)
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表示初始化后先不激活,让别人来激活.
Me.Thread = CreateThread(myNull, dwStackSize, lpfnBasFunc, myNull, dwCreationFlags, lpThreadId)
if Me.Thread = myNull then
Msgbox "create thread failed"
end if
end Sub
下面是两个API用来激活/暂定该线程.
private declare function ResumeThread lib "kernel32"(byval hThread as long)as longprivate declare function SuspendThread lib "kernel32"(byval hThread as long)as long
让我们来用一个变量表示当前线程的状态.
public ThreadStatus as boolean
在vb里,可用property来实现ThreadStatus的管理.
Public property Let Enabled(byval vNewValue as boolean)
if vNewValue = true and Me.ThreadStatus = false then
ResumeThread Me.Thread
Me.ThreadStatus = True
elseif Me.ThreadStatus = true then
SuspendThread Me.Thread
Me.ThreadStatus = False
endif
end Property
这个简单的类可以用New Object来引用:
’make new thread object
dim myThread as New clsThreads
’创建线程 Foo
myThread.Initialize AddressOf Foo
’激活线程
myThread.Enabled = True