简体   繁体   English

Qt / c ++:如果此进程未运行,则启动该进程

[英]Qt/c++ : start a process if this process is not running

I have wrote an app in Qt/c++, The app is currently checking some state and start a new process in case the statement become true. 我已经用Qt / c ++编写了一个应用程序,该应用程序当前正在检查某些状态并开始新的处理,以防语句变为真。

I'm able to start the new process when the statement is true but the app will have to periodically check the statement through a timer. 语句为真时,我可以启动新过程,但应用程序将必须通过计时器定期检查该语句。 I want to make sure that in case the statement become true and and stay at true I will not start the application multiple times. 我想确保在语句变为true并保持为true的情况下,不会多次启动该应用程序。

here is the current code 这是当前代码

QProcess *process = new QProcess();
process->startDetached(fileBrowserExecutablePath);

I know the process can be checked using 我知道可以使用检查流程

process->state() == QProcess::NotRunning

but to check this I need first to start the process otherwise process don't know what to check. 但是要检查这一点,我需要先启动流程,否则流程不知道要检查什么。 I mean which process to check. 我的意思是要检查哪个过程。

Any idea on how to check the process ? 对如何检查过程有任何想法吗?

I have tried to init the process with the application I want to start but it failed to build. 我试图使用我要启动的应用程序来初始化进程,但是无法构建。

Something like : 就像是 :

QProcess *process = new QProcess(fileBrowserExecutablePath);

Thanks 谢谢

You can't do it with Qt(if you didn't create the process with QProcess earlier, of course). 您不能使用Qt(当然,如果您之前没有使用QProcess创建流程)。 You have to use a platform specific API to check if the process is running. 您必须使用特定于平台的API来检查该进程是否正在运行。


But if you did start the process then you can use the QProcess object to check its state. 但是,如果确实启动了该过程,则可以使用QProcess对象检查其状态。 You do it like this: 您可以这样做:

QProcess *process = new QProcess();
process->start(fileBrowserExecutablePath);
///...
p->state() == QProcess::Running

That way you can use the process object to check the state. 这样,您可以使用process对象检查状态。 But note, that if you don't have the process object than you have to use the native API to check if the process is running. 但是请注意,如果没有process对象,则必须使用本机API来检查流程是否正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM