有没有办法获得 ping 的结果,显示在文本框中,但没有旋转的色轮?
Is there a way to get the results of a ping, display in textbox, but not have the spinning colour wheel?
由于 ping 的结果可能需要超过 4 秒(取决于跳数),我得到了旋转的色轮(沙滩球),这通常表示程序已锁定或无法运行。
该应用会继续旋转色轮,并在文本框中显示结果。有没有办法摆脱 "beachball"?
我通过将结果打印到每个变量来进行调试。
沙滩球在 "let handle1 = pipe.fileHandleForReading" 执行后出现。
我尝试了 Grand Central Dispatch (GCD),但仍然得到相同的结果。
// 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
}
我确实在文本框中得到了结果。如果可以做到,最好是在每次 ping 期间在文本框中显示每一行(跃点),而无需 "beachball"。任何帮助将不胜感激。
这就是有效的方法。它将执行一个命令,并且 return 在 textview 或 scrollview window 中的每一跳。这将摆脱旋转 "beachball"。添加了一个计数器或者你可以做一个循环来继续你的许多任务(命令 ping)
func getPing(shellArgs: String)
{
let task = Process()
task.launchPath = "/bin/sh"
task.arguments = ["-c", shellArgs]
let pipe = Pipe()
task.standardOutput = pipe
let outHandle = pipe.fileHandleForReading
outHandle.waitForDataInBackgroundAndNotify()
var obs1 : NSObjectProtocol!
obs1 = NotificationCenter.default.addObserver(forName: NSNotification.Name.NSFileHandleDataAvailable, object: outHandle, queue: nil)
{
notification -> Void in let data = outHandle.availableData
if data.count > 0
{
if let str = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
{
// place data (str) in Results (textview) window
self.Results.stringValue += "\(str)"
}
outHandle.waitForDataInBackgroundAndNotify()
}
else
{
print("EOF on stdout from process")
NotificationCenter.default.removeObserver(obs1)
}
}
var obs2 : NSObjectProtocol!
obs2 = NotificationCenter.default.addObserver(forName: Process.didTerminateNotification, object: task, queue: nil)
{
notification -> Void in print("terminated")
NotificationCenter.default.removeObserver(obs2)
if self.pingCount < 3
{
// add counter and continue for other commands (steps)
self.pingCount += 1
self.cmdPing()
}
else
{
// end task
task.terminate()
}
}
task.launch()
}
由于 ping 的结果可能需要超过 4 秒(取决于跳数),我得到了旋转的色轮(沙滩球),这通常表示程序已锁定或无法运行。
该应用会继续旋转色轮,并在文本框中显示结果。有没有办法摆脱 "beachball"?
我通过将结果打印到每个变量来进行调试。 沙滩球在 "let handle1 = pipe.fileHandleForReading" 执行后出现。 我尝试了 Grand Central Dispatch (GCD),但仍然得到相同的结果。
// 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
}
我确实在文本框中得到了结果。如果可以做到,最好是在每次 ping 期间在文本框中显示每一行(跃点),而无需 "beachball"。任何帮助将不胜感激。
这就是有效的方法。它将执行一个命令,并且 return 在 textview 或 scrollview window 中的每一跳。这将摆脱旋转 "beachball"。添加了一个计数器或者你可以做一个循环来继续你的许多任务(命令 ping)
func getPing(shellArgs: String)
{
let task = Process()
task.launchPath = "/bin/sh"
task.arguments = ["-c", shellArgs]
let pipe = Pipe()
task.standardOutput = pipe
let outHandle = pipe.fileHandleForReading
outHandle.waitForDataInBackgroundAndNotify()
var obs1 : NSObjectProtocol!
obs1 = NotificationCenter.default.addObserver(forName: NSNotification.Name.NSFileHandleDataAvailable, object: outHandle, queue: nil)
{
notification -> Void in let data = outHandle.availableData
if data.count > 0
{
if let str = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
{
// place data (str) in Results (textview) window
self.Results.stringValue += "\(str)"
}
outHandle.waitForDataInBackgroundAndNotify()
}
else
{
print("EOF on stdout from process")
NotificationCenter.default.removeObserver(obs1)
}
}
var obs2 : NSObjectProtocol!
obs2 = NotificationCenter.default.addObserver(forName: Process.didTerminateNotification, object: task, queue: nil)
{
notification -> Void in print("terminated")
NotificationCenter.default.removeObserver(obs2)
if self.pingCount < 3
{
// add counter and continue for other commands (steps)
self.pingCount += 1
self.cmdPing()
}
else
{
// end task
task.terminate()
}
}
task.launch()
}