Qt World Summit: Submit your Presentation

[solved]Running background process using QProcess Continuously

  • hai,

    I am able to run background process using QProcess.
    But my requirement is as below........
    In background I want to run C executable file(c.exe),
    In my C file I have the code with in the while(1) loop.

    in QT
    @process =new QProcess(this);
    process->start(program,arguments); //program .exe file path

    If I write the program like above, Qt creator stopped means it is not closed. it is stopped because the qprocess called the infinite loop.

    even if i remove the line bq. process->waitForFinished();

    also...it is same.

    how can I execute that infinite loop in qt creator to read real time data continuously???????????
    Is the process will work fine with process->waitForFinished(); only or not?

    I don't want to finish the background process, how it is possible?

  • You could use "startDetached":http://qt-project.org/doc/qt-5.0/qtcore/qprocess.html#startDetached
    Than your background application will run forever because of your infinite loop. You would have to open the task manager and the kill the process though.

    Qt creator cannot interfere with a process (also debugging process) when there is no stop. It has to wait for the external debugger until a new response. However, if there will be no response when it is no running across a break point.

    Note: I am not if either response is answering your question. You might to rephrase your post.

  • Another option which adds to what koahnig suggests is to start the c program detached, but in your loop in the c program while, create udp socket (QUdpSocket).

    That way you can send messages between the applications. So when your application closes, you can send a message on exit telling the other listening app to exit as well.

    Of course, only if you have the ability to change the c program.

  • thanks for the reply.

    Koahnig said me What I want exactly. and you said that

    bq. You would have to open the task manager and the kill the process though.

    with out this can I send kill signal to Background c process from my GUI?
    Is it possible?

    and I have a doubt regarding this the process will running continuously fine but the reading data from process-standard output is not displayed in my GUI how can i take those values? is there any function separately?

    thanks in advance

  • I found that, If I start a program using process.start() then I can take the pid value of process but if I start the process using process.startDetached() then it will return only 0.

    then what is the solution for my problem?

  • The "pid":http://qt-project.org/doc/qt-5.0/qtcore/qprocess.html#pid seem to match the windows ID of the process. I never checked though, but I assume that you might use this for killing the process.

    For your other issue, either you are running the process independent of your application with startDetached and have no direct control of its output or you start it as an attached process where you can read the output. This assumes that the process you like to start is completely different from your application and you have to rely on its normal outputs.

    Apparently, you have an application (let's call it App1). This has a GUI where you want to show the output of an independent application (IndepApp). You like to run IndepApp even though App1 is stopped. startDetached is the solution. The problem is that App1 does not receive the output of IndepApp. The solution might be that you are starting another application (App2) which runs IndepApp as an attached process and reading its output with the standard mechanism. You could transfer this output with other means for instance through TCP/IP or a local pipe to App1 for display.

    However, I am not sure if you like something as complex.
    At the time being you use "waitFinished":http://qt-project.org/doc/qt-5.0/qtcore/qprocess.html#waitForFinished This is waiting for up to 30 seconds until the process is finished. But your process is never finishing, since it has a endless loop. So after 30 secs you may collect only the output of these 30 secs. The rest you will never see. So a solution would be to check in a loop more or less regularly whether there is new output in a loop. In such cases you may leave away the waitFinished and revisit the process for checking its output.

  • koahnig said that
    bq. either you are running the process independent of your application with startDetached and have no direct control of its output

    I want to run the process under the control of GUI itself.

  • Hi, the default behavior of QProcess should meet your requirement.

    If it doesn't work, the problem might be in other place of your code.

  • sir my requirement is to run the c executable file as background to my GUI. that c.exe file will read the data from somewhere else.that read data will be again read by GUI and it will be displayed.

    My GUI will work like tool to end user.

  • Consider QtCreator as an exmaple.

    When you clicked the Build or Run, make or nmake.exe will be called through QProcess. at the same time, the output of make will shown in the bottom panel of QtCreator.

  • As 1+1=2 already laid out. Standard QProcess should be good enough for you. You need to have a look to the example stubs provided in the documentation of QProcess. They already give quite helpful details. When you are using the signal of QProcess "eg readyReadStandardOutput":http://qt-project.org/doc/qt-5.0/qtcore/qprocess.html#readyReadStandardOutput QProcess will notify you that there is information to process. So you need to connect this signal to a slot of your application for processing.

  • Standard QProcess means QProcess::start() or QProcess::startDetached()

    and what is the difference between QProcess::readAllStandardOutput() &

  • koahnig, thanks for your reply. almost my problem was solved with your suggestion. next I want to split the data which is continuous. I have the Idea to spilt the data for finite loop but for this infinite loop I am not getting. Can you give any example about this

  • Standard QProcess means QProcess:start in the postings above. The standard case is to have control from your application. With startDetached you do not have this control.

    This "example":http://stackoverflow.com/questions/10098980/real-time-display-of-qprocess-output-in-a-textbrowser respectively the "answer":http://stackoverflow.com/a/10099857 seems to do the job.

    Note: I have not checked it, but looks alright at first glance. Other example you may using google. That is the way I have found this one.

  • Using readyReadStandardOutput() only I am reading the data from qprocess.

    but there is some problem with in milliseconds qprocess reading the data 30000+ times from c-executable file . so I am unable to display the starting data I want to delay in my process I am searched for that one but I am not getting the correct solution
    can you know about this

Log in to reply