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 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)