duanneng2014 2016-04-02 08:36
浏览 437
已采纳

使用Go确定进程/程序是否已终止

I am making a small desktop web application with Go. This application will run as a local web server and a Chrome window will be generated in 'app' mode. The Go program will continue to run the web server during this time.

I need to watch for the moment the user kills this Chrome window, so that the web server can close down too.

I've made a comment below showing where I need assistance.

package main

import (
    "fmt"
    "os/exec"
)

func main(){
    // Setup the application and arguments.
    cmd := "chrome"
    // URL will be local webserver.
    args := []string{"--user-data-dir=c:\\","--window-size=800,600","--app=http://www.google.com"}

    // Start local webserver here.
    // ...

    // Prepare Chrome in app mode.
    cmdExec := exec.Command(cmd, args...);

    // Start Chrome asynchronously.
    cmdExec.Start()

    // Show to the user on the command line that the application is running.
    fmt.Println("Application in progress! Please close webapp to close webserver!")

    // Keep the webserver running, do web app things...

    // Watch for that process we started earlier. If the user closes that Chrome window
    // Then alert the user that the webserver is now closing down.

    // This is where I need help!
    watchForProcessThatWeStartedEarlierForClosure...()//????        

    // And we are done!
    fmt.Println("Application exit!")
}
  • 写回答

1条回答 默认 最新

  • douhuanqiao5290 2016-04-02 09:39
    关注

    You can use the Wait() function on cmdExec to wait for the child process to exit.

    package main
    
    import (
        "fmt"
        "os/exec"
    )
    
    func main(){
        // Setup the application and arguments.
        cmd := "chrome"
        // URL will be local webserver.
        args := []string{"--user-data-dir=c:\\","--window-size=800,600","--app=http://www.google.com"}
    
        // Start local webserver here.
        // ...
    
        // Prepare Chrome in app mode.
        cmdExec := exec.Command(cmd, args...);
    
        // Start Chrome asynchronously.
        cmdExec.Start()
    
        // Show to the user on the command line that the application is running.
        fmt.Println("Application in progress! Please close webapp to close webserver!")
    
        // Keep the webserver running, do web app things...
    
        // Watch for that process we started earlier. If the user closes that Chrome window
        // Then alert the user that the webserver is now closing down.
    
        // Should probably handle the error here
        _ = cmdExec.Wait()      
    
        // And we are done!
        fmt.Println("Application exit!")
    }
    

    Tested it locally with Chromium. After closing the browser window, it takes a few seconds before the Chromium process exists and then Wait() returns.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法