gpt4 book ai didi

c++ - 我的 C++ 控制台应用程序中特定进程的管理不成功

转载 作者:太空宇宙 更新时间:2023-11-04 03:35:47 25 4
gpt4 key购买 nike

在此thread我解决了部分案例,多亏了 NathanOliver,到目前为止我得到了以下代码:

int main(){
//...
bool proc1 = false, proc2 = false, proc3 = false, proc4 = false,
while(true) {
if(!proc1 && ProcessRunning("process1.exe")){
fun1("fun1.bat");
proc1 = true;
}
if(!proc2 && ProcessRunning("process2.exe")){
fun1("fun2.bat");
proc2 = true;
}
if(!proc3 && ProcessRunning("process3.exe")){
fun1("fun3.bat");
proc3 = true;
}
if(!proc4 && ProcessRunning("process4.exe")){
fun1("fun4.bat");
proc4 = true;
}
}
return 0;
}

我仍然无法通过的情况是:

  • 双击 app1 -> process1 开始。
  • process1 运行时,我双击 app2,这样 process2 应该具有与我在第一个线程中提到的相同的行为:

if it finds process2 (the second if(){}), it creates that .bat file and it executes it (kill process2(it might existed before i opened it), start > it again, delete the .bat file generated by fun2(const char name[]){}).


上一篇文章的总结:

int fun1(const char name[]){
ofstream file;
file.open(name, ios::out);
//start of what I write in .bat
file << "@echo off";
file << "cd to specific path";
file << "taskkill /im process.exe* /f";
file << "start process.exe";
file << "del \"%~f0\"";

file.close();
return system(name);
}

其余函数完全相同。

最佳答案

我相信您将 .bat 文件作为同步程序运行,因此直到 bat 无法完成并返回退出代码(您可以将其检查为系统函数的返回值)之前,您的主程序将不会继续运行。您可以在基于 Linux 的系统上使用 fork 使用异步进程,在 Windows 操作系统上使用 CreateProcess。

关于c++ - 我的 C++ 控制台应用程序中特定进程的管理不成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com