【问题标题】:How to terminate a running program in c++如何在 C++ 中终止正在运行的程序
【发布时间】:2015-05-27 09:07:57
【问题描述】:

我用 C++ 编写了一个程序,我称之为

system("C:\xampp\xampp-control.exe");
运行xampp控制面板。当我编译后运行程序时,它运行顺利,除了我写的程序还在运行。一旦 XAMPP 控制面板启动,我想终止程序。有什么可能做的?非常感谢任何帮助。

【问题讨论】:

  • 最好的方案是不使用system() 来启动它。相反,使用任何系统调用来启动新进程而不阻塞,然后只需exit()你的程序。
  • 如果我理解你的问题,也许你可以使用exit (int status);
  • 错误的工作工具:system documentation 和相关引用“并在命令完成后返回”

标签: c++ system exit terminate kill-process


【解决方案1】:

您可以用exec 调用的应用替换您的应用。

// Note: this waits for the exectued program to finish
// before the call to `system()` returns.
system("C:\xampp\xampp-control.exe");

// You can replace the current processes with the one you
// are starting like this.
execl("C:\xampp\xampp-control.exe", "xampp-control.exe");
// If this returns the applicaion failed to start.
std::cerr << "Failed to start application\n";
exit(1);

【讨论】:

  • 非常感谢...用ShellExecute().. 找到了更好的选择.. :)
  • 您能否详细说明为什么它不是一个更好的解决方案? :) 请不要把它当作我在取笑。我是 C++ 的新手。所以我很想知道这里的情况如何......
猜你喜欢
  • 2012-09-07
  • 2018-05-29
  • 1970-01-01
  • 2018-08-12
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
相关资源
最近更新 更多