Platforms to show: All Mac Windows Linux Cross-Platform
/Win/Windows File Copy Threaded
Required plugins for this example: MBS Win Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Win/Windows File Copy Threaded
This example is the version from Fri, 17th Oct 2019.
Project "Windows File Copy Threaded.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
if not TargetWin32 then
MsgBox "This example is for Windows only."
quit
end if
End EventHandler
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
DisableButtons
If CheckBox1.Value Then
thread1.run
else
test1
end if
End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
DisableButtons
If CheckBox1.Value Then
thread2.run
else
test2
end if
End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action()
DisableButtons
If CheckBox1.Value Then
thread3.run
else
test3
end if
End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control ProgressBar1 Inherits ProgressBar
ControlInstance ProgressBar1 Inherits ProgressBar
End Control
Control ProgressWheel1 Inherits ProgressWheel
ControlInstance ProgressWheel1 Inherits ProgressWheel
End Control
Control CheckBox1 Inherits CheckBox
ControlInstance CheckBox1 Inherits CheckBox
EventHandler Sub Action()
Self.MultiThreaded = Me.Value
End EventHandler
End Control
Control Thread1 Inherits Thread
ControlInstance Thread1 Inherits Thread
EventHandler Sub Run()
test1
End EventHandler
End Control
Control Thread2 Inherits Thread
ControlInstance Thread2 Inherits Thread
EventHandler Sub Run()
test2
End EventHandler
End Control
Control Thread3 Inherits Thread
ControlInstance Thread3 Inherits Thread
EventHandler Sub Run()
test3
End EventHandler
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action()
n = n + 1
label2.text = str(n)
End EventHandler
End Control
EventHandler Sub Close()
Dim source As FolderItem
dim dest as FolderItem
source=SpecialFolder.Desktop.Child("source.txt")
dest=SpecialFolder.Desktop.Child("dest.txt")
dest.Delete
source.delete
End EventHandler
EventHandler Sub Open()
WriteTestFile
End EventHandler
Sub DisableButtons()
PushButton1.Enabled = False
PushButton2.Enabled = False
PushButton3.Enabled = False
List.DeleteAllRows
End Sub
Sub EnableButtons()
If app.CurrentThread = Nil Then
// main thread
PushButton1.Enabled = True
PushButton2.Enabled = True
PushButton3.Enabled = True
Else
// call on main thread
xojo.core.Timer.CallLater 0, AddressOf EnableButtons
End If
End Sub
Sub Log(s as string)
System.DebugLog s
If app.CurrentThread = Nil Then
// main thread
List.AddRow s
Else
// call on main thread later
xojo.core.Timer.CallLater(0, AddressOf LogLater, s)
End If
End Sub
Sub LogLater(a as auto)
Dim s As String = a
List.AddRow s
End Sub
Sub Test1()
dim source as FolderItem
dim dest as FolderItem
source=SpecialFolder.Desktop.Child("source.txt")
dest=SpecialFolder.Desktop.Child("dest.txt")
if dest.Exists then dest.Delete
Log "Copy "+source.NativePath+" to "+dest.NativePath
dim w as new WindowsFileCopyMBS
w.MultiThreaded = MultiThreaded
if w.CopyFileSimple(source,dest) then
Log "CopyFile: OK"
else
Log "CopyFile: Failed"
end if
EnableButtons
End Sub
Sub Test2()
dim source as FolderItem
dim dest as FolderItem
source=SpecialFolder.Desktop.Child("source.txt")
dest=SpecialFolder.Desktop.Child("dest.txt")
if dest.Exists then dest.Delete
Log "Copy "+source.NativePath+" to "+dest.NativePath
dim w as new MyWinCopy
w.MultiThreaded = MultiThreaded
if w.CopyFileEx(source,dest,0) then
Log "CopyFileEx: OK"
else
Log "CopyFileEx: Failed"
end if
EnableButtons
End Sub
Sub Test3()
Dim source As FolderItem
dim dest as FolderItem
source=SpecialFolder.Desktop.Child("source.txt")
dest=SpecialFolder.Desktop.Child("dest.txt")
if dest.Exists then dest.Delete
Log "Copy "+source.NativePath+" to "+dest.NativePath
Dim w As New MyWinCopy
w.MultiThreaded = MultiThreaded
w.Parent=self
if w.FileOperationCopy(source,dest,0,"Copying files from REALbasic...") then
Log "FileOperationCopy: OK"
if w.OperationsAborted then
Log "Operations was aborted."
else
Log "Operations was successful."
end if
else
Log "FileOperationCopy: Failed"
end if
EnableButtons
End Sub
Sub WriteTestFile()
dim source as FolderItem
source=SpecialFolder.Desktop.Child("source.txt")
dim t as BinaryStream = BinaryStream.Create(source, true)
t.write "Hello World"
// make a big file. So big, that writing it takes 15 seconds
dim m as new MemoryBlock(100*1000*1000)
dim tt as integer = ticks + 15*60
while ticks<tt
t.Write m
t.Flush
wend
t.Write "end"
t.Close
Log "testfile created: "+str(source.Length)+" Bytes"
End Sub
Property MultiThreaded As Boolean
Property logs() As string
Property n As Integer
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Class MyWinCopy Inherits WindowsFileCopyMBS
EventHandler Function Progress(TotalFileSize as int64, TotalBytesTransferred as int64, StreamSize as int64, StreamBytesTransferred as Int64, StreamNumber as integer, Reason as integer) As integer
dim p as integer = 100*TotalBytesTransferred/TotalFileSize
if p<>last then // reduce number of addrows...
MainWindow.Log "Progress "+str(TotalBytesTransferred)+" of "+str(TotalFileSize)+" = "+Format(TotalBytesTransferred/TotalFileSize,"0%")
last = p
end if
End EventHandler
Property last As Integer
End Class
End Project
See also:
The items on this page are in the following plugins: MBS Win Plugin.