我正在尝试通过程序执行系统命令,然后等待进程终止以继续执行代码指令。我一直在使用 sleep()
但它没有成功,因为它是相对的,我的意思是执行时间因机器而异......那么有什么解决方案吗?
考虑下面的代码(语言==c++):
ShellExecute(0, "open", "cmd.exe","/C rasdial adsl user pwd", 0, SW_HIDE); //can also use system().
Sleep(sec);
if(CheckConnection()) {cout <<"U r connected"; }
等到系统命令被执行以检查连接(我想你现在明白了)。
最佳答案
使用 ShellExecuteEx 而不是 ShellExecute,然后使用您收到的 hProcess 调用 WaitForSingleObject:
SHELLEXECUTEINFO info = { sizeof(SHELLEXECUTEINFO) };
// fill in values in SHELLEXECUTEINFO as necessary
if (ShellExecuteEx(&info))
{
WaitForSingleObject (info.hProcess, INFINITY);
// The new process has now completed
}
else
{
// Launch failed
}
关于c++ - 如何让程序等待命令执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17773855/