简体   繁体   English

在过程返回消息后创建过程并运行代码(C ++)

[英]Create process and run code after the process returns a message (C++)

I am trying to create a new process (which shouldn't block the current program) in C++, and to have the C++ listen for a message. 我试图在C ++中创建一个新进程(不应阻塞当前程序),并让C ++侦听消息。 When the message arrives, I want to run some more code. 消息到达时,我想运行更多代码。

I have this method which executes the command and results the result immediately, but I have no idea how to make one that runs code when the process returns a message: 我有这种方法可以执行命令并立即得到结果,但是我不知道如何在进程返回消息时使它运行代码:

    string exec(const char* cmd)
    {
        // popen for *nix
        FILE* pipe = _popen(cmd, "r");

        if (!pipe)
            return "";

        char buffer[128];
        string result = "";

        while(!feof(pipe)) {
            if(fgets(buffer, 128, pipe) != NULL)
                result += buffer;
        }

        // pclose for *nix
        _pclose(pipe);

        return result;
    }

Note: it may take a 1-3 seconds until the process returns the message -- and the process will continue execution after that so the above code is not sufficient as the executed program will never end. 注意:该过程返回消息之前可能需要1-3秒-并且该过程将继续执行,因此上述代码不足,因为所执行的程序将永远不会结束。

您可以将执行该过程的代码放在单独的线程中,这样,主线程可以继续执行,而辅助线程可以启动该过程,并等待子进程的消息。

After you start the process, periodically check to see if it has sent you the message. 在开始该过程之后,请定期检查它是否已向您发送消息。 If so, call the code you need to call. 如果是这样,请拨打您需要拨打的代码。

Exactly how you do this depends on the structure of the rest of your code. 确切的操作方式取决于其余代码的结构。 For example, if you already have a table of periodic events, then it's very simple. 例如,如果您已经有一个周期性事件表,那么它非常简单。 If you have a 'select' loop type design, it's again very simple. 如果您具有“选择”循环类型的设计,那么它又非常简单。

If you don't have any of those things already, write a 'CheckIfTheresAnythingToDo' function. 如果您还没有这些东西,请编写一个“ CheckIfTheresAnythingToDo”函数。 Have a boolean flag that indicates whether you have a child process running. 有一个布尔标志,指示您是否正在运行子进程。 In 'CheckIfTheresAnythingToDo' return immediately if the flag is clear. 如果标记清除,则在“ CheckIfTheresAnythingToDo”中立即返回。 If it's set, call select and see if the message has been received. 如果已设置,请致电select并查看是否已收到该消息。 If so, call the code you need to call. 如果是这样,请拨打您需要拨打的代码。 (I'd recommend not using standard I/O because it doesn't play well with the non-blocking semantics you need to do more than one thing at a time.) (我建议您不要使用标准I / O,因为它不能很好地与非阻塞语义配合使用,您一次只能做一件事情。)

Then just call CheckIfTheresAnythingToDo at key points in your code. 然后只需在代码中的关键点调用CheckIfTheresAnythingToDo If you have some kind of loop structure, call it once on each pass of the loop. 如果您具有某种循环结构,则在循环的每一遍都调用一次。 If you have code that waits for things, have your wait function call it. 如果您有等待代码的代码,请让您的wait函数调用它。

But this is the kind of thing you should already know how to do when you started your design. 但这是开始设计时就应该知道的方法。 "I need to check something every once in a while" is a very typical requirement and if your overall design has no way to accommodate it, you should rethink that design. “我需要不时检查一下”是一个非常典型的要求,如果您的整体设计无法容纳它,则应重新考虑该设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM