[SOLVED]How to prevent multiple instances of application
-
I want to allow only a single instance of my application and an attempt to run multiple instance should call a function from the running instance of the program. Is it possible in Qt.
-
QSharedMemory and QSystemSemaphore are your friends to pass IPC messages between multiple instances of an application. Just use a unique key for your application. If an instance can't create() a new QSharedMemory for that key, you know that another instance is already running. In that case the second instance can attach() to the existing QSharedMemory and write some message to it - the "main" instance will then process the message. The QSystemSemaphore is required to synchronize the read/write operations on the shared memory. I use one read and one write semaphore to implement a simple FIFO pipe in the shared memory.
-
-
QtSingleApplication class does exactly what i want, i am surprised why it is not included in the Qt SDK. The examples are running fine, but when i try the same with my own application, the project compiles but does not run, it simply crashes without a clue!
The following code works fine:
@#include <QtGui/QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv); manager w; if(argc==2) { w.durl=argv[1]; w.on_actionAdd_Url_triggered(); } w.show(); return a.exec();
}@
But when i use QtSingleApplication class the application crashes without starting
@#include <QtGui/QApplication>
#include "manager.h"int main(int argc, char *argv[])
{
QtSingleApplication a(argc, argv);
if (a.isRunning())
return 0;
manager w;
if(argc==2)
{
w.durl=argv[1];
w.on_actionAdd_Url_triggered();
}
w.show();
a.setActivationWindow(&w);return a.exec();
}@
The debugger points to the line
@manager w;@ -
It worked! thanks a lot. There was a problem in my code
-
You're welcome. Please feel free to prepend your initial title with [Solved] to indicate that there is a solution inside.