启动新的可执行文件后 C++ 程序不退出

C++ program is not exiting after starting new executable file

考虑两个 C++ 项目:

项目 1:

// projectOne.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Sleep(5000);
    system("projectTwo.exe");
    return 0;
}

项目 2:

// projectTwo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Sleep(5000);
    system("projectOne.exe");
    return 0;
}

我寻求的行为是:projectOne 开始 => 开始 projectTwo => projectOne 结束 => projectTwo 将开始 projectOne => projectTwo结束 => projectOne 将开始 projectTwo

然而,节目并没有结束。比如projectOne开始projectTwo,当return 0;projectOne里面是运行的时候,不会结束projectOne。所以几分钟后,会同时出现多个版本的程序运行ning。我以为它与 system 命令有关。也许它等到项目完成,直到进入下一行代码,这导致了循环,但我不确定。我怎样才能解决这个问题?我需要程序在使用 system 命令调用其中一个程序后结束。我希望这个问题很清楚。

您的方法会陷入无限循环,并且不会结束!!

You are spawning multiple instances of projectOne and projectTwo which in turn are creating more.. It's recursive -_-

编辑

System WAITS!


解决方案

int execl(char * pathname, char * arg0, arg1, ..., argn, NULL);

system 阻塞 运行ning 线程直到 system returns 并且 system 不会 return 直到执行的进程终止.

有很多方法可以解决这个问题。最简单且最有可能可移植的是 use a std::thread 到 运行 system 在与主处理线程 运行 并发的线程中。

std::thread procthread([processToRun] {system(processToRun.c_str());});
procthread.detach(); 

任何调用 system 的东西都可以是简短、甜美且便携的。第一行创建一个线程并在提供的进程名称上执行 运行s system 的 lambda 函数。第二行断开线程与 std::thread 对象的连接,并允许线程 运行 空闲。否则,如果 procthread 超出范围,线程将被终止,并且很可能会发生不好的事情。

如果因为您的开发系统不支持 C++11 或更高版本而无法执行此操作,您可以使用特定于操作系统的线程,但如果您必须使用特定于系统的线程创建调用,您可能以及使用系统特定的 process 创建调用来直接创建新进程。

在POSIX系统中,posix_spawn will likely be the go-to function. I don't have a machine at my disposal to test this on at the moment, so I'll just link to Starting a process using posix_spawn

在 Windows、use CreateProcess or your variant of choice. The following code is based on Microsoft's Creating Processes documentation page 下并修改为稍微不那么特定于 Microsoft,并且在继续执行之前不等待生成的进程完成。

char processToRun[] = "process to run"; //NOTE: Not a std::string!
STARTUPINFO si;
PROCESS_INFORMATION pi;

memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
memset(&pi, 0, sizeof(pi));

// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
                   processToRun, // Command line DANGER! won't accept const char* 
                                 // cannot use std::string::c_str
                   NULL, // Process handle not inheritable
                   NULL, // Thread handle not inheritable
                   FALSE, // Set handle inheritance to FALSE
                   0, // No creation flags
                   NULL, // Use parent's environment block
                   NULL, // Use parent's starting directory
                   &si, // Pointer to STARTUPINFO structure
                   &pi)) // Pointer to PROCESS_INFORMATION structure
{
    std::cerr << "CreateProcess failed ("<<GetLastError()<<").\n";
    return false;
}

// do stuff
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return true;