精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● VB和Basic>>〓〓..API 函数使用..〓〓>>进程和线程>>用API实现在VB的多线程

主题:用API实现在VB的多线程
发信人: starspace(ss)
整理人: winsy(2003-03-05 15:25:28), 站内信件
近来看到几个帖子是问多线程的,现附以下代码。
(呵呵,我也不知道那是我自己写的,还是抄来的~~可能一半抄一半改的)

多线程实现方法
Public Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Public Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
Public Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode As Long)
Dim hThread As Long, idThread As Long
Public Sub StartThread(ByVal i As Long)
    idThread = 0
    hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf WaitTime, ByVal i&, ByVal 0&, idThread)
    If hThread = 0 Then MsgBox ("Can;t start thread")
End Sub
Public Function StopThread() As Long
    Do
        Call GetExitCodeThread(hThread, StopThread)
        DoEvents
    Loop While StopThread = STILL_ACTIVE
    CloseHandle hThread
    hThread = 0
End Function

注意:在编译环境下和EXE模式上效果会不同,在编译模式下容易非法操作。
在线程中,不能用DoEvents。可用Sleep。
好象不必在退出时关闭打开的线程,
可以多次调用StartThread来达到多线程,局部变量各自独立。


----
*******以下是本人的灌水原则*******
~~~ 1:不在游泳池里灌水。~~~
=== 2:不在油里灌水。    ===  

[关闭][返回]