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

首頁 > 開發 > 綜合 > 正文

使用Visual Basic 6實現真正實用的多線程處理

2024-07-21 02:25:11
字體:
來源:轉載
供稿:網友
 

事實上使用visual basic的activex exe技術可以輕松的實現多線程處理.

原理先不說了,先舉個示例,有興趣的話大家可以一起討論

1.新建一個工程,類型選擇activex exe,工程重命名為testexe
在工程中添加一個form,放上一個timer控件.
將class1改名為clstest,注意其instancing要設置為5-multiuse, 以下是其代碼:

option explicit
private declare sub sleep lib "kernel32" (byval dwmilliseconds as long)
private withevents otimer as timer
private frmtmp as form1
private ltotalloop as long
private bdostop as boolean
private linterval as long

public event progress(byval lprogress as long)


public sub startsub(byval ltotal as long)
    ltotalloop = ltotal
    otimer.enabled = true
end sub

public sub stopsub()
    bdostop = true
end sub

private sub class_initialize()
    set frmtmp = new form1
    load frmtmp
    set otimer = frmtmp.timer1
    otimer.enabled = false
    bdostop = false
    linterval = 1
end sub

private sub doloop()
    dim i as long
    for i = 0 to ltotalloop
        sleep (linterval)
        raiseevent progress(i)
       
        if bdostop = true then
            exit sub
        end if
       
    next
end sub

private sub class_terminate()
    unload frmtmp
    set frmtmp = nothing
    set otimer = nothing
end sub

private sub otimer_timer()
    otimer.enabled = false
    doloop
end sub

public property get lmillisecs() as long
    lmillisecs = linterval
end property

public property let lmillisecs(byval vnewvalue as long)
    linterval = vnewvalue
end property

在testexe property(工程屬性)的threading model中,設置thread per object,或者選擇thread pool中設置大于1的數值.如把thread pool設置為2, 則調用此activex exe最多能同時有兩個線程, 更多的請求將放置于隊列中.
編譯testexe

下面來測試我們的多線程程序:

新建一個standard exe工程,在reference中選擇剛剛做好的testexe.exe
在form1中添加兩個listbox,兩個commandbutton,command1為開始,command2為停止
以下是project1.form1的代碼:

option explicit
private withevents otest1 as testexe.clstest
private withevents otest2 as testexe.clstest

private sub command1_click()
    set otest1 = new testexe.clstest
    otest1.lmillisecs = 100
    otest1.startsub (1000)
   
    set otest2 = new testexe.clstest
    otest2.lmillisecs = 100
    otest2.startsub (1000)
end sub

private sub command2_click()
    otest1.stopsub
    otest2.stopsub
end sub

private sub form_unload(cancel as integer)
    set otest1 = nothing
    set otest2 = nothing
end sub

private sub otest1_progress(byval lprogress as long)
    list1.additem lprogress
    list1.listindex = list1.listcount - 1
end sub

private sub otest2_progress(byval lprogress as long)
    list2.additem lprogress
    list2.listindex = list2.listcount - 1
end sub

啟動project1,點擊command1,怎么樣,看見來效果了嗎? 試著把testexe的thread pool改成1看看會怎么樣? 這是我認為最簡單且穩固的多線程實現方法了,大家有什么好的想法歡迎留言.



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江油市| 韩城市| 原阳县| 天镇县| 光泽县| 改则县| 巴青县| 临朐县| 德昌县| 新巴尔虎左旗| 威远县| 迭部县| 巴中市| 水富县| 罗田县| 裕民县| 蕉岭县| 商南县| 江山市| 通海县| 和平区| 黄冈市| 大埔县| 鲁甸县| 阿拉尔市| 芒康县| 宾川县| 防城港市| 祁连县| 花垣县| 邹平县| 十堰市| 陆丰市| 万荣县| 武山县| 阳朔县| 哈巴河县| 泰顺县| 金乡县| 绥德县| 清水县|