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. (从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 ifend 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 endifend Property
这个简单的类可以用New Object来引用:
'make new thread objectdim myThread as New clsThreads'创建县城 FoomyThread.Initialize AddressOf Foo'激活县城myThread.Enabled = True