Implement multiple threads in VB!

xiaoxiao2021-03-06  70

'Features: Create a multi-thread class for initialization threads. Class name: CLS_THREAD

'Parameters: longpointFunction is used to receive function address values ​​passing from the main adjustment process.

'Calling method: 1. Declaration thread class object variable DIM Mythread As CLS_THREAD

'2. Call form: with mythread

'.Initialize addressof Custom Process or Function Name' (Initialization Thread).

'.Threadenabled = true' (set whether the thread is activated)

'End with

'3. Termination call: set mythread = Nothing

'Email: lixun007@163.net

'Test On: VB6.0 Win2000 and VB6.0 WinXP IT's Pass!

Option expedition

'Creating a thread API

'This API is transformed, and lpthReadAttributes are changed to the ANY type, and LPSTARTADDRESS is changed to the pass value:

'Because the entry address of the function is passed by a den range, if the address is used to pass the address of the parameter rather than the entrance address of the function

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

'Termination Thread API

Private Declare Function TerminateTHRead Lib "Kernel32" (Byval DwExitcode As Long) AS Long

'Activation thread API

Private Declare Function ResumeThread LIB "Kernel32" (Byval Hthread As Long) As Long

'Hang up thread API

Private Declare Function SuspendThread LIB "Kernel 32" (Byval Hthread As Long) AS Long

Private const create_suspended = & h4 'thread hang constant

'Custom Thread Structure Type

Private Type Udtthread

Handle As Long

Enabled as boolean

End Type

Private metheard as udtthread

'Initialization thread

Public Sub Initialize (Byval LongpointFunction As Long)

DIM Longstacksize As Long, LongcreationFlags As Long, LPTHREADID AS Long, Longnull As Long

ON Error ResMe next

Longnull = 0

Longstacksize = 0longCreationFlags = CREATE_SUSPENDED 'Create a thread and hang, activate the thread by the program

'Creating a thread and returns a handle

Metheard.handle = Createthread (Longnull, Longstacksize, ByVal LongpointFunction, Longnull, LongcreationFlags, LPTHREADID)

If Metheard.Handle = Longnull Then

MsgBox "thread creation failed!", 48, "Error"

END IF

End Sub

'Get the thread to activate attributes

Public property get threadenabled () as boolean

ON Error ResMe next

Enabled = metheard.enabled

End Property

'Set whether the thread activates attributes

Public property let threadenabled (Byval newValue as boolean)

ON Error ResMe next

'If the active thread (NewValue is true), the thread is activated when this thread is not activated.

IF newValue and (not metheard.enabled) THEN

ResumeThread Metheard.Handle

Metheard.enabled = True

Else 'If the thread is activated (true) and this thread is activated, this thread is hangned.

If metheard.enabled then

Suspendthread Metheard.Handle

Metheard.enabled = false

END IF

END IF

End Property

'Termination thread event

Private sub coplass_terminate ()

ON Error ResMe next

Call TerminateThread (Metheard.handle, 0)

End Sub

转载请注明原文地址:https://www.9cbs.com/read-89949.html

New Post(0)