Vbs multi-thread download implementation code

Source: Internet
Author: User

It is easy to practice and initially implement the "multi-thread" download that I think. (For multithreading, refer to the link on the 12th floor)
To avoid redundancy, some error checks are saved. I don't think it's very useful. If you are interested, study and discuss it together. Thank you for your correction:

Copy codeThe Code is as follows: 'by wankoilz

Url = InputBox ("complete input :")
ThreadCount = InputBox ("the number of input threads (no more than 10, too much will be cumbersome ):")
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
'Get the file size
FileSize = ohttp. getResponseHeader ("Content-Length ")
Ohttp. abort
'Create a temporary file of the same size as the downloaded file for the following ado segment Rewriting
Fso. CreateTextFile (filePath & "TmpFile", True, False). Write (Space (fileSize ))
Ado. LoadFromFile (filePath & "TmpFile ")

BlockSize = Fix (fileSize/threadCount): remainderSize = fileSize-threadCount * blockSize
Upbound = threadCount-1
'Define an array containing msxml2.xmlhttp objects. The number of members is the number of threads.
'Direct Dim array name (variable name) is not acceptable. Execute is used here to change it.
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
'Multipart download
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
'Every time a thread finishes downloading, it is written to the corresponding location of the temporary file.
Ado. Position = I * blockSize
MsgBox "Thread" & I & "Download complete! "
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
'Set according to file size
MsgBox "30 seconds timeout! "
WScript. Quit
End If
Loop
If fso. FileExists (filePath & fileName) Then fso. DeleteFile (filePath & fileName)
Fso. DeleteFile (filePath & "TmpFile ")
Ado. SaveToFile (filePath & fileName)
MsgBox "file downloaded! "

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

Test:Copy codeCode: http://www.jb51.net/images/logo.gif

VBS Multithreading

Someone emailed me a question today:

May I ask if the INPUTBOX function in VBS can be closed upon timeout?
If yes, how should I close the input box after timeout? Thank you very much.

At first glance, this is impossible, because the InputBox function itself does not have a timeout to close the parameter, and the program will continue to run until the InputBox returns, and the subsequent statements cannot be executed before the InputBox returns.

If VBS can implement multithreading in advanced languages ...... It is a pity that VBS cannot implement multithreading, but the setTimeout method can be used to simulate "multithreading ".

Copy codeThe Code is as follows: 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

Use the setTimeout method to set a 3-second timeout. After 3 seconds, use the SendKeys method to send the ESC key to end the InputBox. Of course, using SendKeys is unreliable. I usually seldom use the SendKeys method because it makes too many assumptions. What if InputBox is not an activation window? Here, SendKeys is used for simple programs. Instead, it can be replaced by the end script itself.

Similarly, if you want to implement the Timer event in VB in VBS, you can use the setInterval method. I will not write an example and read the document myself.

Reference link: setTimeout Method (window, Window Constructor)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.