Since the results to a ping can take more than 4 seconds (depending on the amount of hops), I get the spinning colour wheel (beachball) which normally indicates that the program has locked up or not functioning.
The app does continue with the spinning colour wheel, and finishes with the results in the textbox. Is there a way to get rid of the "beachball"?
I Debugged by printing out the results to each variable. The beachball shows up after the "let handle1 = pipe.fileHandleForReading" has executed. I tried Grand Central Dispatch (GCD) but still get the same results.
// Built using X-Code Version 8.2.1 - Swift Version 3.0.2
// Text view for results (display)
@IBOutlet weak var Results: NSTextField!
// Start button to execute ping
@IBAction func startButton(_ sender: Any)
{
cmdPing()
}
// Global variables
var myArg = ""
var shellResults = ""
func cmdPing()
{
// Step 1
myArg = "ping -c 10 www.google.com"
shellResults = getPing(shellArgs: myArg)
Results.stringValue = shellResults
// Step 2
myArg = "ping -c 10 127.0.0.1"
shellResults = getPing(shellArgs: myArg)
Results.stringValue = shellResults
}
// function that executes a shell command and returns its value (myArg)
func getPing(shellArgs: String) -> String
{
let task:Process = Process()
let pipe:Pipe = Pipe()
task.launchPath = "/bin/sh"
task.arguments = ["-c", shellArgs]
task.standardOutput = pipe
task.launch()
let handle1 = pipe.fileHandleForReading
let data1 = handle1.readDataToEndOfFile()
let result1 = NSString(data: data1, encoding: String.Encoding.utf8.rawValue)
let trimmed1 = (result1! as NSString).trimmingCharacters(in: NSCharacterSet.whitespaces)
shellResults = trimmed1
task.waitUntilExit()
return shellResults
}
I do get the results in the textbox. What would be nice if it can be done would be to show each line (hop) in the textbox during each ping without the "beachball". Any help would be appreciated.