首页 > 解决方案 > 如何在 C++ 中打开和关闭子应用程序

问题描述

我正在为 ubuntu 编写一个 c++ 控制台应用程序,它充当速率表控制器来接收用户命令并通过 tcp 将它们发送到执行命令并返回信息(例如位置和速度)的表。

我编写了一个 js/electron 应用程序来充当位置和速度信息的 GUI。

我希望我的用户能够在控制台应用程序中输入“GUION”来打开 GUI。然后输入“GUIOFF”关闭它。使用system("cd rtgui; npm start");我可以打开 GUI,它运行得非常好 - 但这不是最佳选择,因为我无法使用“GUIOFF”命令关闭它,因为相同的终端窗口再次无法访问(据我所知)。

我的问题是,有没有比system()我可以访问同一个终端窗口来调用npm start然后npm stop在用户需要时杀死 GUI 更好的方法?

命令代码在这里。

/*************************************************
 appCommand - checks if the input is a command for this app. 
*************************************************/
bool control::appCommand(string userInput){
    // TODO - RUN THIS COUT IN DEBUG MODE ONLY
    //printf("\nchecking command: %s\n", userInput.c_str());
    
// if "HELP" send help list and return true
    if(userInput == std::string("HELP")){
        helpSection();
        return true; 
    } // if the gui is not enabled, populate it and switch the bool to true
    else if(userInput == std::string("GUION")){
        if(guiEnabled == false){
            printf("\nEnabling GUI.\n");

            system("cd rtgui; npm start");

            // switch bool
            guiEnabled = true;
        }
        // is an app command
        return true;
    } // if the gui is showing, close it.
    else if(userInput == std::string("GUIOFF")){
        if(guiEnabled == true){
            printf("\nDisabling GUI.\n");

            // TODO - Enter PROPER code to close GUI here.
            system("cd rtgui; npm stop");

            // switch bool
            guiEnabled = false;
        }
        // is an app command
        return true;
    }
    else if(userInput == string("QUIT")){
        // We want to close connections - set connected to false.
        isConnected = false;
        // is an app command
        return true;
    }

    return false;
}

标签: c++

解决方案


推荐阅读