Qt single instance app how to Manage on crash
-
Hi,
I want to execute my application as single instance, currently I am using QSharedMemory and working fine. I am using Qt5.2.1 on Ubuntu 12.04. below is my test code,
@ QApplication a(argc, argv);
a.processEvents();const char* MEM_KEY = "56"; QSharedMemory sharedMem(MEM_KEY); if( !sharedMem.create( 512, QSharedMemory::ReadWrite) ) { QMessageBox msgBox; msgBox.setText( QObject::tr("Can't start more than one instance of the application.") ); msgBox.setIcon( QMessageBox::Critical ); msgBox.exec(); exit(0); } MainWindow w; w.show(); int p=0; //p=p/0; // create exception here return a.exec();@
But the if I made the application crash(as shown in above code) and start the application again it showing @Can't start more than one instance of the application@ means the previous instance is still there even if it's crashed. It should not happened in my case, how can I restart my application in such a situation?.
Thanks
Haris -
Try using QtSingleApplication: "link":https://qt.gitorious.org/qt-solutions/qt-solutions/source/38e79e3f04d6fd5a3df585a60b2aec95e8e68368:qtsingleapplication
It is not part of Qt, but is created an maintained by Qt Company, so it's very close.
-
Or you can do like me.
Check if .exe is running ... :@
//in main => MainWidget m; m.show();
MainWidget::MainWidget(int argc, char** argv, QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
initialisations();#ifdef ONLY_ONE_INSTANCE
checkOnlyOneInstance();
#endif
}
@
checkOnlyOneInstance
@
void MainWidget::checkOnlyOneInstance()
{
QProcess x;
x.start("tasklist");
x.waitForStarted(200); //1000 avant
x.waitForReadyRead(200);
x.waitForFinished(200);
QString result(x.readAllStandardOutput());
if(result.count("MON APPLICATION.exe",Qt::CaseInsensitive) > 1 )
{
popupMessage(tr("L'Application est déjà en cours d'execution!"),2000,this);
QTimer::singleShot(2000, qApp, SLOT(quit()));
}
}
@It works for me.
-
Hi,
Beware, you are doing your initialization twice. You're application should not start any initialization before checking it's not already running. Also that's a Windows specific implementation and the OP is running Ubuntu. sierdzio's suggestion is good. It works very well.
-
[quote author="Francknos" date="1424334416"]My Initialisation is just to config the GUI ... (stylesheet ...)[/quote]
Yes, but it does start the application. So, for a tiny bit of time, you do have 2 instances of your application launched.
-
[quote author="SGaist" date="1424300270"]Hi,
Beware, you are doing your initialization twice. You're application should not start any initialization before checking it's not already running. Also that's a Windows specific implementation and the OP is running Ubuntu. sierdzio's suggestion is good. It works very well.[/quote]
I have tried this but it doesn't work with me.
-
What doesn't work ?
-
You could also use D-Bus in stead of shared memory.
-
you also can try to create and use a specific singleton class.
look at C++ codes/exemples for learn how to use well singleton class.
i do it for check login and users access/privileges.
after that, you can call your singleton by handle the "get_instance" method (for exemple). -
[quote author="jerome_isAviable?" date="1424746717"]you also can try to create and use a specific singleton class.[/quote]
That won't work for this use case. Singleton has a single instance of a class per application - but there can be many applications launched. The OP wants to run only one instance of the application.
-
exact.