国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

在VB中實現多線程!

2024-07-21 02:20:52
字體:
來源:轉載
供稿:網友
'功能:創建多線程類,用于初始化線程。 類名:cls_thread

'參數:longpointfunction 用于接收主調過程傳遞過來的函數地址值

'調用方法:1.聲明線程類對象變量 dim mythread as cls_thread

' 2.調用形式:with mythread

' .initialize addressof 自定義過程或函數名 '(初始化線程) .

' .threadenabled = true '(設置線程是否激活)

' end with

' 3.終止調用: set mythread = nothing

' email:[email protected]

' test on: vb6.0+win2000 and vb6.0+winxp it's pass !



option explicit

'創建線程api

'此api經過改造,lpthreadattributes改為any型,lpstartaddress改為傳值引用:

'因為函數的入口地址由形參變量傳遞,如果用傳址那將傳遞形參變量的地址而不是函數的入口地址

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

'終止線程api

private declare function terminatethread lib "kernel32" (byval hthread as long, byval dwexitcode as long) as long

'激活線程api

private declare function resumethread lib "kernel32" (byval hthread as long) as long

'掛起線程api

private declare function suspendthread lib "kernel32" (byval hthread as long) as long



private const create_suspended = &h4 '線程掛起常量



'自定義線程結構類型

private type udtthread

handle as long

enabled as boolean

end type



private metheard as udtthread

'初始化線程

public sub initialize(byval longpointfunction as long)

dim longstacksize as long, longcreationflags as long, lpthreadid as long, longnull as long

on error resume next

longnull = 0

longstacksize = 0

longcreationflags = create_suspended '創建線程后先掛起,由程序激活線程



'創建線程并返線程句柄

metheard.handle = createthread(longnull, longstacksize, byval longpointfunction, longnull, longcreationflags, lpthreadid)



if metheard.handle = longnull then

msgbox "線程創建失敗!", 48, "錯誤"

end if

end sub



'獲取線程是否激活屬性

public property get threadenabled() as boolean

on error resume next

enabled = metheard.enabled

end property



'設置線程是否激活屬性

public property let threadenabled(byval newvalue as boolean)

on error resume next

'若激活線程(newvalue為真)設為true且此線程原來沒有激活時激活此線程

if newvalue and (not metheard.enabled) then

resumethread metheard.handle

metheard.enabled = true

else '若激活線程(newvalue為真)且此線程原來已激活則掛起此線程

if metheard.enabled then

suspendthread metheard.handle

metheard.enabled = false

end if

end if

end property



'終止線程事件

private sub class_terminate()

on error resume next

call terminatethread(metheard.handle, 0)

end sub




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 星子县| 弥勒县| 东乌珠穆沁旗| 资阳市| 广水市| 左云县| 延寿县| 太和县| 玉山县| 大田县| 北票市| 杭锦后旗| 松滋市| 宁强县| 东乌珠穆沁旗| 铁岭市| 洛南县| 景宁| 五莲县| 桓仁| 秀山| 永吉县| 张北县| 磐安县| 当雄县| 荥经县| 临清市| 驻马店市| 嵩明县| 师宗县| 桦川县| 府谷县| 鄂托克旗| 丁青县| 威远县| 日土县| 扶绥县| 财经| 扶绥县| 沛县| 车致|