Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin, MBS MacCocoa Plugin, MBS MacOSX Plugin
Last modified Mon, 11th May 2025.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /MacOSX/DispatchIO Test
Download this example: DispatchIO Test.zip
Project "DispatchIO Test.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits DesktopWindow
Control PickInputButton Inherits DesktopButton
ControlInstance PickInputButton Inherits DesktopButton
EventHandler Sub Pressed()
Var f As FolderItem = GetOpenFolderItem("")
If f <> Nil Then
InputFile = f
NSPathControlInput.view.File = f
CopyXojoButton.Enabled = True
CopyDispatchButton.Enabled = true
End If
End EventHandler
End Control
Control PickOutputButton Inherits DesktopButton
ControlInstance PickOutputButton Inherits DesktopButton
EventHandler Sub Pressed()
Var f As FolderItem = GetSaveFolderItem("", "test")
If f <> Nil Then
OutputFile = f
NSPathControlOutput.view.File = f
End If
End EventHandler
End Control
Control NSPathControlInput Inherits DesktopNSPathControlControlMBS
ControlInstance NSPathControlInput Inherits DesktopNSPathControlControlMBS
EventHandler Sub Action()
End EventHandler
End Control
Control NSPathControlOutput Inherits DesktopNSPathControlControlMBS
ControlInstance NSPathControlOutput Inherits DesktopNSPathControlControlMBS
EventHandler Sub Action()
End EventHandler
End Control
Control CopyXojoButton Inherits DesktopButton
ControlInstance CopyXojoButton Inherits DesktopButton
EventHandler Sub Pressed()
If CopyThread.ThreadState = Thread.ThreadStates.NotRunning Then
Log "Start copy thread."
ProgressBar1.Value = 0
ProgressWheel1.Visible = True
StartTime = Microseconds
CopyThread.run
Else
Log "Copy thread runs already."
End If
End EventHandler
End Control
Control CopyDispatchButton Inherits DesktopButton
ControlInstance CopyDispatchButton Inherits DesktopButton
EventHandler Sub Pressed()
DispatchStart
End EventHandler
End Control
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
End Control
Control ProgressBar1 Inherits DesktopProgressBar
ControlInstance ProgressBar1 Inherits DesktopProgressBar
End Control
Control CopyThread Inherits Thread
ControlInstance CopyThread Inherits Thread
EventHandler Sub Run()
Var bi As BinaryStream = BinaryStream.Open(InputFile)
Var bo As BinaryStream = BinaryStream.Create(OutputFile, True)
Var Len As Integer = bi.Length
LastProgress = 0
While Not bi.EOF
bo.Write bi.Read(1024 * 1024 * 10)
Var p As Integer = 100 * bi.Position / bi.Length
If p <> LastProgress Then
Var d As New Dictionary
d.Value("progress") = p
LastProgress = p
Me.AddUserInterfaceUpdate d
End If
Wend
bi.Close
bo.Close
// report finished
Var d As New Dictionary
d.Value("message") = "Copy done."
d.Value("progress") = 100
Me.AddUserInterfaceUpdate d
EndTime = Microseconds
d = New Dictionary
d.Value("message") = Str((EndTime-StartTime)/1000000.0, "0.0")+" seconds"
Me.AddUserInterfaceUpdate d
End EventHandler
EventHandler Sub UserInterfaceUpdate(data() as Dictionary)
For Each d As Dictionary In data
Var progress As Integer = d.Lookup("progress", -1)
If progress >= 0 Then
If progress <> ProgressBar1.Value Then
ProgressBar1.Value = progress
If progress = 100 Then
ProgressWheel1.Visible = False
End If
End If
End If
Var message As String = d.Lookup("message", "")
if message <> "" then
Log message
End If
Next
End EventHandler
End Control
Control ProgressWheel1 Inherits DesktopProgressWheel
ControlInstance ProgressWheel1 Inherits DesktopProgressWheel
End Control
Control InfoLabel Inherits DesktopLabel
ControlInstance InfoLabel Inherits DesktopLabel
End Control
EventHandler Sub Opening()
Var f As New FolderItem("/tmp/test", folderitem.PathModes.Native)
OutputFile = f
NSPathControlOutput.view.File = f
NSPathControlOutput.view.focusRingType = NSViewMBS.NSFocusRingTypeNone
NSPathControlInput.view.focusRingType = NSViewMBS.NSFocusRingTypeNone
End EventHandler
Sub DispatchFinished()
DispatchIORead.Close
DispatchIOWrite.Close
'Var m4 As Double = Microseconds
'System.DebugLog "Async: "+Str((m4-m3)/1000000.0, "0.0")+" seconds"
// report finished
ProgressBar1.Value = 100
ProgressWheel1.Visible = false
Log "Dispatch copy done."
EndTime = Microseconds
Log Str((EndTime-StartTime)/1000000.0, "0.0")+" seconds"
End Sub
Sub DispatchReadDone(DispatchIO as DispatchIOMBS, offset as Int64, Data as DispatchDataMBS, done as Boolean, Error as Integer)
'System.DebugLog CurrentMethodName
If Error <> 0 Then Break
Var bytesRead As Integer = data.Size
if DispatchOffset = 0 then
System.DebugLog "got " + data.Size.ToString + " at offset " + DispatchOffset.ToString
End If
// let's output what we got to
DispatchIOWrite.Write(DispatchOffset, data, AddressOf DispatchWriteDone)
DispatchOffset = DispatchOffset + bytesRead
If done Then
ReadDone = True
Log "Done reading."
End If
End Sub
Sub DispatchStart()
Log "Dispatch copy starts"
StartTime = Microseconds
Var flags As Integer = DispatchIOMBS.kOFlagCreate + DispatchIOMBS.kOFlagTruncate + DispatchIOMBS.kOFlagWriteOnly
DispatchSize = InputFile.Length
DispatchOffset = 0
LastProgress = 0
ProgressWheel1.Visible = True
'm3 = Microseconds
DispatchIORead = New DispatchIOMBS(DispatchIOMBS.kTypeStream, InputFile, 0)
DispatchIOWrite = New DispatchIOMBS(DispatchIOMBS.kTypeStream, OutputFile, flags)
DispatchIORead.LowWater = 1024 * 1024 * 10
DispatchIORead.HighWater = 1024 * 1024 * 50
// let's ask to read whole file
DispatchIORead.Read(0, 10000000000000, AddressOf DispatchReadDone)
End Sub
Sub DispatchWriteDone(DispatchIO as DispatchIOMBS, offset as Int64, OriginalData as DispatchDataMBS, RemainingData as DispatchDataMBS, done as Boolean, Error as Integer)
If Error <> 0 Then Break
If done Then
Var Size As Integer = OriginalData.Size
'System.DebugLog "wrote "+Size.ToString+" bytes at "+offset.ToString
Var pos As Integer = Size + offset
Var p As Integer = 100 * pos / DispatchSize
If p <> LastProgress Then
LastProgress = p
ProgressBar1.Value = p
End If
If Pos = DispatchSize Then
DispatchFinished
End If
End If
End Sub
Sub Log(s as string)
System.DebugLog s
List.addrow s
List.ScrollPosition = List.LastAddedRowIndex
End Sub
Property DispatchIORead As DispatchIOMBS
Property DispatchIOWrite As DispatchIOMBS
Property DispatchOffset As Integer
Property DispatchSize As Integer
Property EndTime As Double
Property InputFile As FolderItem
Property LastProgress As Integer
Property OutputFile As FolderItem
Property ReadDone As Boolean
Property StartTime As Double
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem WindowMenu = "Window"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
End Project
Download this example: DispatchIO Test.zip
The items on this page are in the following plugins: MBS MacOSX Plugin.