PDA

View Full Version : How to check if a program is running?



raphaelf
23rd June 2008, 11:30
Hi everybody,

how could i check if a program is running?

I think one of following functions is what i am searching, but i have not found a example and i dont know how to check if a program is running..

Can somebody make a example for me? :o
QProcess::started () or QProcess::state ()

wysota
23rd June 2008, 11:32
What program? "Current" program, arbitrary program or a program started with QProcess?

raphaelf
23rd June 2008, 11:43
Hi,

I am starting a process:


QProcess *process = new QProcess(this);
QString program = "psexec.exe \\\\stchps426 -c -e -f -n 6 i:\\WinZip8.1.exe";
process->start(program);


This exe runs between 5 seconds and one minute.

I have to wait until this exe stop to make some job

wysota
23rd June 2008, 12:44
Rely on signals then. You'll get notified when the process is started and finished.

raphaelf
23rd June 2008, 13:51
Hi Wysota,

thanks

i solved with QProcess::waitForFinished :)

raphaelf
24th June 2008, 09:14
oh,

this function is blocking my app :crying:

Have i another posibility to check if a exe is running?

wysota
24th June 2008, 09:20
Yes, read my previous post again.

raphaelf
24th June 2008, 09:39
I dont know how to implement..

Where can i find a example? :o

lyuts
24th June 2008, 10:25
QProcess has the following signal


void finished(int exitCode, QProcess::ExitStatus exitStatus)


And you can connect it to your slot



connect(yourProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(yourSlot(int, QProcess::ExitStatus)));


and then implement your slot


void yourClass::yourSlot(int exitCode, QProcess::ExitStatus exitSatus)
{
// your code
}

raphaelf
24th June 2008, 14:23
hi everybody,

what is wrong:

.h:


#include "ui_mainwindow.h"
#include <QProcess>



class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();


public slots:
void yourSlot(int *exitCode, QProcess::ExitStatus *exitSatus);


private:
Ui::PushFast ui;
};


.cpp:


MainWindow::MainWindow()
{
ui.setupUi(this);
QProcess *process = new QProcess(this);
connect(*process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(yourSlot(int, QProcess::ExitStatus)));

}

void MainWindow::yourSlot(int *exitCode, QProcess::ExitStatus *exitSatus)
{
QMessageBox::information(this, "", "process finished");
}

void MainWindow::startpsexec()
{
QString username = ui.username_le->text();
QString password = ui.password_le->text();
QString hostname = ui.hostname_le->text();
QString software = ui.software_cb->currentText();
QString path = ui.source_path_cb->currentText();



//Installieren
//QProcess *process = new QProcess(this);
QString zeichen = "\"";
QString program = "psexec.exe \\\\" + hostname + " -u " + hostname + "\\" + username + " -p " + password + " -i -c -e -f -n 6 " + zeichen + path + "\\" + software + zeichen;
process->start(program);
QString information = process->readAllStandardError();
information.replace(0, 126, "");
information.replace("with error code 0.", "");

}

jacek
24th June 2008, 22:05
process->start(program);
QString information = process->readAllStandardError();
You have to give some time to psexec, before you read what it has written. Either connect so readyReadStandardError() signal or use waitForFinished() (note that the latter will hang your application, so it's suitable only if psexec works really fast).

wysota
24th June 2008, 23:04
hi everybody,

what is wrong:


public slots:
void yourSlot(int *exitCode, QProcess::ExitStatus *exitSatus);



connect(*process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(yourSlot(int, QProcess::ExitStatus)));

Those signatures don't match.

raphaelf
25th June 2008, 08:25
Hi all,

thanks very much it works :p

nhs_0702
29th April 2010, 11:41
Hi all,

thanks very much it works :p

why my source code error?

http://www.mediafire.com/?kdryz5tyozj




#include "widgetcheckprocess.h"
#include <QMessageBox>
WidgetCheckProcess::WidgetCheckProcess(QWidget *parent, Qt::WFlags flags): QMainWindow(parent, flags)
{
ui.setupUi(this);
qpProcess = new QProcess;
StartProcess("I:/SoftWare_Install/wincmp-setup.exe");
connect(qpProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(SlotDetectFinish(int, QProcess::ExitStatus)));
}
WidgetCheckProcess::~WidgetCheckProcess()
{
}
void WidgetCheckProcess::StartProcess(QString qsProcessPath)
{
qpProcess->startDetached(qsProcessPath);
qpProcess->waitForFinished();
}
void WidgetCheckProcess::SlotDetectFinish(int *exitCode, QProcess::ExitStatus *exitSatus)
{
QMessageBox::information(this, "", "process finished");
}




#ifndef WIDGETCHECKPROCESS_H
#define WIDGETCHECKPROCESS_H

#include <QtGui/QMainWindow>
#include "ui_widgetcheckprocess.h"
#include <QProcess>
class WidgetCheckProcess : public QMainWindow
{
Q_OBJECT
public:
QProcess *qpProcess;
WidgetCheckProcess(QWidget *parent = 0, Qt::WFlags flags = 0);
~WidgetCheckProcess();
void StartProcess(QString qsProcessPath);
public slots:
void SlotDetectFinish(int *exitCode, QProcess::ExitStatus *exitSatus);
//signals:
// void finished(int exitCode, QProcess::ExitStatus exitStatus);
private:
Ui::WidgetCheckProcessClass ui;
};

#endif // WIDGETCHECKPROCESS_H




#include "widgetcheckprocess.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
WidgetCheckProcess w;
w.show();
return a.exec();
}

wysota
29th April 2010, 13:49
How to ask smart questions (http://www.catb.org/~esr/faqs/smart-questions.html)

nhs_0702
30th April 2010, 15:39
Hi all,

thanks very much it works :p

you can help me,why i use SIGNAL(finished(int, QProcess::ExitStatus) like you but cant run
http://www.mediafire.com/?kdryz5tyozj

squidge
30th April 2010, 15:59
Please re-read wysota's post and stop duplicating your posts.