簡體   English   中英

避免 QProcess 被殺死 (QProcess: Destroyed while process is still running)

[英]Avoid QProcess being killed (QProcess: Destroyed while process is still running)

我試圖運行這段代碼:

QProcess process;
process.setWorkingDirectory("D:\\Programs\\Qt\\Units\\MyJavaProjects\\StackExp\\target");
process.setProgram("java.exe");
process.setArguments({"-jar","StackExp-1.0-SNAPSHOT.jar"});
process.start();

和 cmd 不會打開,也不會執行。 它只顯示此消息:

QProcess: Destroyed while process ("java.exe") is still running

請問,誰知道怎么回事? 以及如何在 QProcess 中使用 cmd 運行 my.jar 文件?

您可能在QProcess完成之前調用它的析構函數,這會殺死文檔中提到的進程。 請注意,當process退出 scope 時,將調用析構函數。

存在不同的解決方案:

  1. 等待進程完成: waitForFinished

     process.waitForFinished (-1); // -1 = no time out
  2. 在棧上構造QProcess

     QProcess *process = new QProcess(); ...

    請注意,您應該在完成后破壞該進程以避免 memory 泄漏。 在構造期間指定parent級可能有助於自動管理QProcess的生命周期。

  3. 以分離模式啟動進程: startDetached

     ... process.startDetached ();

    如果調用進程退出,分離的進程將繼續運行而不受影響。

    也可以使用QProcess::startDetached的 static 重載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM