Xojo Conferences

Platforms to show: All Mac Windows Linux Cross-Platform

/CURL/CURLS preemptive threaded download
Required plugins for this example: MBS CURL Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /CURL/CURLS preemptive threaded download
This example is the version from Sat, 1st Jan 2016.
Project "CURLS preemptive threaded download.rbp"
Filetype text
End FileTypes
Class Window1 Inherits Window
Control EditField1 Inherits TextArea
ControlInstance EditField1 Inherits TextArea
End Control
Control List Inherits ListBox
ControlInstance List Inherits ListBox
End Control
Control StartButton Inherits PushButton
ControlInstance StartButton Inherits PushButton
EventHandler Sub Action() ProgressWheel1.Visible = True Run End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control StopButton Inherits PushButton
ControlInstance StopButton Inherits PushButton
EventHandler Sub Action() ProgressWheel1.Visible = false for each t as DownloadThread in Threads t.Cancel next End EventHandler
End Control
Control ProgressWheel1 Inherits ProgressWheel
ControlInstance ProgressWheel1 Inherits ProgressWheel
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action() for each t as DownloadThread in threads if t.DoneMessageNew then List.cell(t.listindex, 0) = t.DoneMessage t.DoneMessageNew = false elseif t.d <> nil then dim p as Double = t.d.ProgressPercent List.cell(t.listindex, 0) = "Download "+Format(p/100.0, "0%") end if next End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
EventHandler Sub Close() // if something is still running, cancel it for each t as DownloadThread in threads t.cancel next System.DebugLog CurrentMethodName+" Waiting for threads to finish..." // now wait for all threads to end while true app.YieldToNextThread for each t as DownloadThread in threads 'System.DebugLog "Thread: "+str(t.ThreadID)+" has state: "+str(t.state) if t.State <> thread.NotRunning then // wait longer Continue while end if next // exit loop as all threads stopped 'System.DebugLog "exit loop." exit wend System.DebugLog CurrentMethodName+": All Theads finished." 'dim n as integer = 0 'for each t as DownloadThread in threads 'n = n + 1 'System.DebugLog "Thread: "+str(t.ThreadID)+" has state: "+str(t.state) 'next 'System.DebugLog str(n)+" Threads checked, "+str(UBound(Threads)+1)+" in total" End EventHandler
Protected Sub run() dim s as string dim lines(-1),line as string dim w as DownloadThread s=EditField1.text s=ReplaceLineEndings(s,EndOfLine) lines=Split(s,EndOfLine) for each line in lines line=trim(line) if line<>"" then List.AddRow line w=new DownloadThread w.url=line w.listindex=List.LastIndex w.run Threads.Append w end if next End Sub
Property threads() As DownloadThread
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
End Class
Class DownloadThread Inherits Thread
EventHandler Sub Run() dim e as integer dim s as string dim f as FolderItem dim b as BinaryStream s=NthField(url,"/",CountFields(url,"/")) f = SpecialFolder.Desktop.Child("CURL Test - "+s) d = new CURLSMBS d.OptionURL=url d.YieldTime=true d.OptionFollowLocation = true d.OptionMaxRedirs = 3 d.OptionVerbose=true if not d.CreateMTOutputFile(f) then Break // failed to create output file end if f = SpecialFolder.Desktop.Child("CURL Debug - "+s+".txt") if not d.CreateMTDebugOutputFile(f) then Break // failed to create debug file end if e=d.PerformMT d.CloseMTDebugOutputFile d.CloseMTInputFile if d.Cancel then f.Delete end if DoneMessage = "Finished. "+Format(d.GetInfoSizeDownload/1024,"0")+" kilobytes" DoneMessageNew = true d = nil End EventHandler
Sub Cancel() if d<>Nil then // if something is running, cancel it d.Cancel = true end if End Sub
Property DoneMessage As string
Property DoneMessageNew As Boolean
Property d As CURLSMBS
Property listindex As integer
Property url As string
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS CURL Plugin.

MBS Xojo Chart Plugins