vbs 多線程下載實(shí)現(xiàn)代碼

字號(hào):


    話說(shuō)還是閑來(lái)練手,初步實(shí)現(xiàn)了自己認(rèn)為的“多線程”下載。(至于是不是多線程,可以參考12樓鏈接)
    為避免冗余,省了一些錯(cuò)誤檢查。我覺得沒(méi)多大實(shí)際用途,有興趣的兄弟一起學(xué)習(xí)討論唄。歡迎大家指正:
    代碼如下:
    'by wankoilz
    url=InputBox("輸入完整下載地址:")
    threadCount=InputBox("輸入線程數(shù)(不超過(guò)10吧,太多就累贅了):")
    fileName=GetFileName(url)
    filePath=GetFilePath(WScript.ScriptFullName)
    Set ohttp=CreateObject("msxml2.xmlhttp")
    Set ado=CreateObject("adodb.stream")
    Set fso=CreateObject("scripting.filesystemobject")
    ado.Type=1
    ado.Mode=3
    ado.Open
    ohttp.open "Head",url,True
    ohttp.send
    Do While ohttp.readyState<>4
    WScript.Sleep 200
    Loop
    '獲得文件大小
    fileSize=ohttp.getResponseHeader("Content-Length")
    ohttp.abort
    '創(chuàng)建一個(gè)和下載文件同樣大小的臨時(shí)文件,供下面ado分段重寫
    fso.CreateTextFile(filePath&"TmpFile",True,False).Write(Space(fileSize))
    ado.LoadFromFile(filePath&"TmpFile")
    blockSize=Fix(fileSize/threadCount):remainderSize=fileSize-threadCount*blockSize
    upbound=threadCount-1
    '定義包含msxml2.xmlhttp對(duì)象的數(shù)組,·成員數(shù)量便是線程數(shù)
    '直接 Dim 數(shù)組名(變量名) 是不行的,這里用Execute變通了一下
    Execute("Dim arrHttp("&upbound&")")
    For i=0 To UBound(arrHttp)
    startpos=i*blockSize
    endpos=(i+1)*blockSize-1
    If i=UBound(arrHttp) Then endpos=endpos+remainderSize
    Set arrHttp(i)=CreateObject("msxml2.xmlhttp")
    arrHttp(i).open "Get",url,True
    '分段下載
    arrHttp(i).setRequestHeader "Range","bytes="&startpos&"-"&endpos
    arrHttp(i).send
    Next
    Do
    WScript.Sleep 200
    For i=0 To UBound(arrHttp)
    If arrHttp(i).readystate=4 Then
    '每當(dāng)一個(gè)線程下載完畢就將其寫入臨時(shí)文件的相應(yīng)位置
    ado.Position=i*blockSize
    MsgBox "線程"&i&"下載完畢!"
    ado.Write arrHttp(i).responseBody
    arrHttp(i).abort
    complete=complete+1
    End If
    Next
    If complete=UBound(arrHttp)+1 Then Exit Do
    timeout=timeout+1
    If timeout=5*30 Then
    '根據(jù)文件大小設(shè)定
    MsgBox "30秒超時(shí)!"
    WScript.Quit
    End If
    Loop
    If fso.FileExists(filePath&fileName) Then fso.DeleteFile(filePath&fileName)
    fso.DeleteFile(filePath&"TmpFile")
    ado.SaveToFile(filePath&fileName)
    MsgBox "文件下載完畢!"
    Function GetFileName(url)
    arrTmp=Split(url,"/")
    GetFileName=arrTmp(UBound(arrTmp))
    End Function
    Function GetFilePath(fullname)
    arrTmp=Split(fullname,"\")
    For i=0 To UBound(arrTmp)-1
    GetFilePath=GetFilePath&arrTmp(i)&"\"
    Next
    End Function
    測(cè)試下載地址:
    代碼如下:
    http://pic02.newdu.com/uploads/202504/02/logo3552.gif
    VBS實(shí)現(xiàn) 多線程 補(bǔ)充
    今天有人發(fā)郵件問(wèn)我一個(gè)問(wèn)題:
    想請(qǐng)教一下VBS中INPUTBOX函數(shù)能否超時(shí)關(guān)閉?
    如果可以的話,應(yīng)該如何超時(shí)關(guān)閉輸入框? 萬(wàn)分感謝
    乍一看這是不可能實(shí)現(xiàn)的,因?yàn)镮nputBox函數(shù)本身沒(méi)有超時(shí)關(guān)閉的參數(shù),而且程序會(huì)一直等待InputBox返回才繼續(xù)運(yùn)行,后面的語(yǔ)句不可能在InputBox返回之前執(zhí)行。
    如果VBS能實(shí)現(xiàn)高級(jí)語(yǔ)言的多線程的話……只可惜VBS不可能實(shí)現(xiàn)多線程,但是可以用setTimeout方法模擬“多線程”。
    代碼如下:
    Dim IE
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "about:blank"
    Set window = IE.Document.parentWindow
    id = window.setTimeout(GetRef("on_timeout"),3000,"VBScript")
    name = InputBox("Please enter your name","InputBox Timeout")
    window.clearTimeout id
    If name <> "" Then MsgBox "Hello," & name
    IE.Quit
    'By Demon
    'http://demon.tw
    Sub on_timeout()
    Dim WshShell
    set WshShell = CreateObject("wscript.Shell")
    WshShell.SendKeys "{ESC}"
    End Sub
    用setTimeout方法設(shè)定3秒超時(shí),3秒后用SendKeys方法發(fā)送ESC鍵結(jié)束InputBox。當(dāng)然,用SendKeys是很不靠譜的,我一般很少用SendKeys方法,因?yàn)樗隽颂嗟募僭O(shè),萬(wàn)一InputBox不是激活窗口呢?這里只是為了程序簡(jiǎn)單而用了SendKeys,可以換成結(jié)束腳本本身。
    同理,想在VBS中實(shí)現(xiàn)VB中的Timer事件的話可以用setInterval方法,我就不寫例子了,自己看文檔。