Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Go确定进程/程序是否已终止_Go - Fatal编程技术网

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

使用Go确定进程/程序是否已终止,go,Go,我正在用Go制作一个小型桌面web应用程序。此应用程序将作为本地web服务器运行,并将在“应用程序”模式下生成一个Chrome窗口。Go程序将在此期间继续运行web服务器 我需要观察一下用户关闭这个Chrome窗口的那一刻,这样web服务器也可以关闭 我在下面做了一个评论,说明我在哪里需要帮助 package main import ( "fmt" "os/exec" ) func main(){ // Setup the application and argume

我正在用Go制作一个小型桌面web应用程序。此应用程序将作为本地web服务器运行,并将在“应用程序”模式下生成一个Chrome窗口。Go程序将在此期间继续运行web服务器

我需要观察一下用户关闭这个Chrome窗口的那一刻,这样web服务器也可以关闭

我在下面做了一个评论,说明我在哪里需要帮助

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!")
}
可以使用cmdExec上的函数等待子进程退出

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!")
}
用铬局部测试。关闭浏览器窗口后,Chromium进程需要几秒钟的时间,然后等待()返回。

您可以使用cmdExec上的函数等待子进程退出

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!")
}

用铬局部测试。关闭浏览器窗口后,Chrome进程需要几秒钟的时间,然后等待()返回。

考虑到Chrome的多进程架构,这可能有点棘手。也许网页会定期通过ajax向服务器发送ping,如果没有收到ping,则会超时?考虑到Chrome的多进程架构,这可能有点棘手。也许网页会定期通过ajax向服务器发送ping,如果没有收到ping,则会超时?