I try to resume a downloaded file, it works with this code but it take a very long time to initialise, i don't know why.
Start the program then wait a moment like 30% and restart.
Thanks to help me

Code: Select all
Enumeration
#Window_0
#pourcentage
#ProgressBar_0
#Boutton_0
EndEnumeration
Global Quit.b
Global Close.b
Procedure update()
DownloadURL.s = "http://www.purebasic.com/download/PureBasic_Demo.exe"
DownloadFilename.s= "PureBasic_Demo.exe"
Protected hInet, hURL, Bytes
Protected BufferLength = 2048, Buffer.s = Space(BufferLength)
Protected Url.s = DownloadURL.s
Protected Filename.s = DownloadFilename.s
Protected File
Protected CurrentSize, PreviousSize, FileSize, time, BytesPerSecond
Protected Domain.s, String.s, i, BufferLengthWas = BufferLength
Protected hInetCon, hHttpOpenRequest, iretval
hInet = InternetOpen_("Downloader",0,0,0,0)
hURL = InternetOpenUrl_(hInet,Url.s,0,0,$80000000,0)
;Get filesize
Domain.s = StringField(Url.s,3,"/")
hInetCon = InternetConnect_(hInet,Domain.s,80,#Null,#Null,3,0,0)
If hInetCon
hHttpOpenRequest = HttpOpenRequest_(hInetCon,"HEAD",ReplaceString(Url.s,"http://"+Domain.s+"/",""),#Null,#Null,0,$80000000,0)
If hHttpOpenRequest
iretval = HttpSendRequest_(hHttpOpenRequest,#Null,0,0,0)
If iretval
HttpQueryInfo_(hHttpOpenRequest,19,@Buffer.s,@BufferLength,0) ;changes the buffer length
String.s = PeekS(@Buffer.s,BufferLength): BufferLength = BufferLengthWas
If Trim(String.s) = "200"
HttpQueryInfo_(hHttpOpenRequest,22,@Buffer.s,@BufferLength,0)
String.s = PeekS(@Buffer.s,BufferLength): BufferLength = BufferLengthWas
If FindString(String.s,"Content-Length:",1)
i = FindString(String.s,"Content-Length:",1) + Len("Content-Length:")
String.s = Mid(String.s,i,Len(String.s)-i)
FileSize = Val(Trim(String.s))/1024
EndIf
EndIf
EndIf
EndIf
EndIf
;Download file and update status
If hURL
taille_fichier = FileSize("PureBasic_Demo.exe")
If taille_fichier > 0 ;le fichier existe déjà alors reprise
CurrentSize = taille_fichier
File = OpenFile(#PB_Any, "PureBasic_Demo.exe") ;ouverture du fichier
FileSeek(File, CurrentSize)
InternetSetFilePointer_(hURL, CurrentSize, 0, #FILE_BEGIN ,0)
Else ;nouveau téléchargement
File = CreateFile(#PB_Any,Filename.s)
EndIf
If File
time = ElapsedMilliseconds()
While InternetReadFile_(hURL,@Buffer.s,BufferLength,@Bytes) And Bytes > 0
If Quit = #True
Break
EndIf
WriteData(File,@Buffer.s,Bytes)
CurrentSize + Bytes
Pourcentage.f = ((CurrentSize/1024)/FileSize)*100
SetGadgetState(#ProgressBar_0, Pourcentage)
If time < ElapsedMilliseconds() - 1000
time = ElapsedMilliseconds()
BytesPerSecond = CurrentSize - PreviousSize
PreviousSize = CurrentSize
EndIf
Wend
CloseFile(File)
EndIf
EndIf
InternetCloseHandle_(hURL)
InternetCloseHandle_(hInetCon)
InternetCloseHandle_(hInet)
Close = #True
EndProcedure
If OpenWindow(#Window_0, 0, 0, 250, 105, "Download", #PB_Window_ScreenCentered)
If CreateGadgetList(WindowID(#Window_0))
ProgressBarGadget(#ProgressBar_0, 21, 30, 207, 20, 0, 100)
ButtonGadget(#Boutton_0, 95, 77, 60, 20, "Abord")
EndIf
EndIf
CreateThread(@update(), 0)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget ;si on est dans un gadget
Select EventGadget()
Case #Boutton_0
Quit = #True
EndSelect
EndSelect
Until Close = #True
End