【问题标题】:Is there a way to get the results of a ping, display in textbox, but not have the spinning colour wheel?有没有办法获得 ping 的结果,显示在文本框中,但没有旋转色轮?
【发布时间】:2019-11-02 15:59:31
【问题描述】:

由于 ping 的结果可能需要超过 4 秒(取决于跳数),我得到了旋转的色轮(沙滩球),这通常表明程序已锁定或无法运行。

应用程序会继续旋转色轮,并以文本框中的结果结束。有没有办法摆脱“沙滩球”?

我通过将结果打印到每个变量来进行调试。 沙滩球在“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 期间在文本框中显示每一行(跳跃),而不使用“沙滩球”。任何帮助将不胜感激。

【问题讨论】:

标签: swift cmd process ping


【解决方案1】:

这是有效的。它将执行一个命令并返回文本视图或滚动视图窗口中的每一跳。这将摆脱旋转的“沙滩球”。添加了一个计数器或者你可以做一个循环来继续你的许多任务(命令 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()
}

【讨论】:

    猜你喜欢
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2018-01-09
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多