Xojo Conferences

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/readToEndOfFileInBackgroundAndNotify Test
Required plugins for this example: MBS MacBase Plugin, MBS MacCocoa Plugin, MBS MacOSX Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/readToEndOfFileInBackgroundAndNotify Test
This example is the version from Fri, 10th Oct 2013.
Project "readToEndOfFileInBackgroundAndNotify Test.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() xThread = New ThreadTest xThread.Run() tmrCheck.Mode = Timer.ModeMultiple End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control TextArea1 Inherits TextArea
ControlInstance TextArea1 Inherits TextArea
End Control
Control tmrCheck Inherits Timer
ControlInstance tmrCheck Inherits Timer
EventHandler Sub Action() If xThread.State = Thread.NotRunning Then Dim sTest As string = xThread.FinalResult //Should have lots of stuff! TextArea1.Text = sTest 'BREAK me.Mode = Timer.ModeOff xThread = Nil End If End EventHandler
End Control
Property xThread As ThreadTest
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class ThreadTest Inherits Thread
EventHandler Sub Run() Dim xTask As New TaskKSW xTask.Run("/usr/bin/find",Array("/Applications/Utilities")) me.FinalResult = xTask.Result End EventHandler
Property FinalResult As String
End Class
Class MyNSNotificationObserverMBS Inherits NSNotificationObserverMBS
EventHandler Sub GotNotification(notification as NSNotificationMBS) Select Case Notification.Name Case NSFileHandleMBS.NSFileHandleReadToEndOfFileCompletionNotification Dim xUserInfo as Dictionary = Notification.UserInfo Dim xData As NSFileHandleMBS = notification.objectVariant Dim sData As String = xUserInfo.Value(NSFileHandleMBS.NSFileHandleNotificationDataItem) If me.DataForHandle.HasKey(xData.Handle) Then 'Dim sExisting As String = me.DataForHandle.Value(xData.Handle) 'Break Else me.DataForHandle.Value(xData.Handle) = sData End if End Select End EventHandler
Sub Constructor() // Calling the overridden superclass constructor. Super.Constructor me.DataForHandle = New Dictionary End Sub
Property DataForHandle As Dictionary
End Class
Class TaskKSW
Private Sub ReadInTimer(tt as timer) xPipe = New NSPipeMBS xPipeError = New NSPipeMBS me.Result = "" FTool = PathToFolderItemMBS(toolpath) If FTool = Nil or Not FTool.Exists Then me.Result = "Failed to execute the command was the tool was not found: " + toolpath HadError = true Return End If xFileHandleStandard = xPipe.fileHandleForReading xFileHandleError = xPipeError.fileHandleForReading xTask.LaunchPath = toolpath xTask.setArguments(toolargs) xTask.setStandardOutput(xPipe) xTask.setStandardError(xPipeError) Try xTask.Launch() Catch xError As NSExceptionMBS me.Result = xError.Reason +EndOfLine + EndOfLine + _ xError.Name HadError = true Return End Try xFileHandleStandard.readToEndOfFileInBackgroundAndNotify() xFileHandleError.readToEndOfFileInBackgroundAndNotify() End Sub
Sub Run(_toolpath As String, _toolargs() As String, ChangeEncoding As Boolean = True) me.ToolPath = _toolpath me.ToolArgs = _toolargs HadError = False me.SetupObservers() Dim xRunLoop as NSRunLoopMBS = NSRunLoopMBS.currentRunLoop xRunLoop.AddDummyPort // required to keep xRunLoop looping xRunLoop.Run(0.01) me.SetupObservers() xTask = New NSTaskMBS xTheTimer = New Timer xTheTimer.Period = 0 xTheTimer.Mode = Timer.ModeSingle AddHandler xTheTimer.Action, AddressOf ReadInTimer Do Until Not xTask.isRunning If HadError Then me.ErrorCode = -1 Return End If Call xRunLoop.runMode(NSRunLoopMBS.NSDefaultRunLoopMode, 0.01) Loop //Obtain Result Do Until xFileHandleStandard<>nil and Observer.DataForHandle.HasKey(xFileHandleStandard.Handle) Call xRunLoop.runMode NSRunLoopMBS.NSDefaultRunLoopMode, 0.01 Loop me.Result = Observer.DataForHandle.Value(xFileHandleStandard.Handle) If ChangeEncoding Then me.Result = DefineEncoding(me.Result,Encodings.UTF8) me.Result = ReplaceLineEndings(me.Result,EndOfLine) End If //Obtain Error Dim sError As String Do Until Observer.DataForHandle.HasKey(xFileHandleError.Handle) Call xRunLoop.runMode(NSRunLoopMBS.NSDefaultRunLoopMode, 0.01) Loop sError = Observer.DataForHandle.Value(xFileHandleError.Handle) If ChangeEncoding Then sError = DefineEncoding(sError,Encodings.UTF8) sError = ReplaceLineEndings(sError,EndOfLine) End If Dim iReason As Integer = xTask.terminationReason me.ErrorCode = xTask.terminationStatus If me.ErrorCode <> 0 and Len(me.Result) = 0 Then me.Result = sError Exception xError As NSExceptionMBS me.Result = xError.Reason +EndOfLine + EndOfLine + _ xError.Name me.HadError = True me.ErrorCode = -1 Return Finally xTask = Nil xFileHandleError = nil xFileHandleStandard = nil xPipe = Nil xPipeError = nil xTheTimer = nil End Sub
Private Sub SetupObservers() if Center <> Nil Then Return Center = NSNotificationCenterMBS.defaultCenter Observer = New MyNSNotificationObserverMBS Center.addObserver(Observer, NSFileHandleMBS.NSFileHandleReadToEndOfFileCompletionNotification,xFileHandleStandard) Center.addObserver(Observer, NSFileHandleMBS.NSFileHandleReadToEndOfFileCompletionNotification,xFileHandleError) 'Center.addObserver(Observer, NSFileHandleMBS.NSFileHandleReadCompletionNotification,xFileHandleStandard) 'Center.addObserver(Observer, NSFileHandleMBS.NSFileHandleReadCompletionNotification,xFileHandleError) End Sub
Property Private Center As NSNotificationCenterMBS
Property ErrorCode As Integer
Property Private FTool As FolderItem
Property HadError As Boolean
Property Observer As MyNSNotificationObserverMBS
Property Result As String
Property Private ToolArgs() As String
Property Private ToolPath As String
Property Private xFileHandleError As NSFileHandleMBS
Property Private xFileHandleStandard As NSFileHandleMBS
Property Private xPipe As NSPipeMBS
Property Private xPipeError As NSPipeMBS
Property Private xTask As NSTaskMBS
Property Private xTheTimer As Timer
End Class
End Project

Feedback, Comments & Corrections

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

MBS Xojo tutorial videos