Hello everyone,
Would somebody know some way of see if your app is already running when you start it and therefore stop the launching?
I work with Qt4.3.2 on XP Sp2.
Thanks a lot ...
Hello everyone,
Would somebody know some way of see if your app is already running when you start it and therefore stop the launching?
I work with Qt4.3.2 on XP Sp2.
Thanks a lot ...
There's a commercial solution for this, from Trolltech: http://trolltech.com/products/qt/add...leapplication/.
Otherwise, you have to find platform dependent solutions. For windows you can search codeproject.com and for Mac there are probably solutions on developer.apple.com.
linux apps often solve this problem by creating a lock file containing the process id of the running application. If another instance of the app is launched it will check if such a lock file exists. if not everything is ok and the app starts and creates its own lock file. If there already exists a pid lock file, the new app extracts the process id from the file and checks whether the app is still running (the pid is still valid). If it is the app will exit, if not the app will start and overwrite the lock file with its own process id. The application should delete their corresponding pid on exit. If it crashes and thus cannot delete the lockfile, this approach still works due to the checking of the state of the pid.
The "lock file" runs great
I was thinking doing something like that but with a socket instead. I think with a file is better.
If somebody is interested I,ve done this in the entry point of the app:
Qt Code:
int main(int argc, char *argv[]) { Csec csec(argc, argv); if (lockFile.exists() && !lockFile.remove()) { return 0; } return 0; } MainWindow *mainWindow = new MainWindow; mainWindow->show(); int rtn = csec.exec(); lockFile.close(); return rtn; }To copy to clipboard, switch view to plain text mode
Last edited by jpn; 16th December 2007 at 17:15. Reason: missing [code] tags
hvw59601 (16th December 2007)
With this approach if your application crashes or is killed, you won't be able to restart it. You forgot to implement the second part - checking the pid contained in the file.
This case is covered inIf the one app is running the second app won't be able to remove the lock.Qt Code:
if (lockFile.exists() && !lockFile.remove())To copy to clipboard, switch view to plain text mode
I don't know if holding the open file handler while the app is running is good style but it seems OK for me.
I don't think this is a foulproof approach... For instance under Windows if an application holding a file open crashes, files opened with mandatory locking (which seems to be the default case on Windows when opening for writing) can't be opened again (or removed) by another process. This is a tricky situation... The best possible solution is to use shared memory or a similar ipc mechanism (sockets are fine too although they trash the system a bit).
Well, after some time playing with it the method is running OK everytime.
A good appreciation.The better way is to use absolute path.................. from different shourtcuts for example
I make that test. I aborted the program before close the file and the app could be restarted later. I suppose that the SO itself close the file if the PID is dead.....if your application crashes or is killed, you won't be able to restart it.....
Anyway, you can know what is your PID with the QProcess:id() method, but how can you discover what other processes are running in the system?????? I can´t find this functionality anywhere.
Another two observations:
- I edited the piece of code because the line Csec csec(argc, argv); has to be the first line. Otherwise the first "alert" don´t run. I suppose this is because the event loop must be running for dialogs to run.
- In fact, the only one sentence that really do all the work is this:
if (lockFile.exists() && !lockFile.remove())
because if i start the app lots of times this sentence always goes well
if (!lockFile.open(QIODevice::Truncate | QIODevice::WriteOnly))
It seems like lots of processes could open a file in write mode, but fortunately they can´t
delete it if it´s opened by another one.
Last edited by simper66; 16th December 2007 at 17:42.
Hi,
This is my code (which is similar to the above) and works fine:
In the main.cpp program:
Qt Code:
QString arq; #ifdef Q_OS_WIN32 arq="C:\\windows\\system32\\lock.ctl"; #else arq="/var/run/lock.ctl"; #endif if(dlock.exists()) { ".this programs already is running", return -1; } else { out << "running" << endl; dlock.close(); } mw->show(); return app.exec();To copy to clipboard, switch view to plain text mode
In the MainWindow subclass, make follow:
Create a eventClose(QEventClose *event) protected funcion like this.
This funcion will be remove the lock file when the user end the application.
Qt Code:
{ if(!isWindowsActived() ) { QString arq; #ifdef Q_OS_WIN32 arq="qcompras.ctl"; #else arq="lock.ctl"; #endif d.remove(arq); event->accept(); close(); } else event->ignore(); }To copy to clipboard, switch view to plain text mode
Last edited by wysota; 17th December 2007 at 11:58. Reason: missing [code] tags
I'am use QtSingleApplication
http://doc.trolltech.com/solutions/4...plication.html
Sample:
Qt Code:
int main(int argc, char *argv[]) { //QApplication::setStyle (new QPlastiqueStyle()); //QApplication a(argc, argv); QtSingleApplication a("iTMHSM",argc, argv); if (a.sendMessage("12345")) return 0; a.initialize(); HSM w; w.show(); &w, SLOT(once(const QString&))); a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit())); return a.exec(); }To copy to clipboard, switch view to plain text mode
Bookmarks