PDA

View Full Version : Single Application Instance



BadKnees
20th March 2009, 11:59
I want to share this simple solution if it is a good idea, or get some response if it is a bad idea. It is a way to check if application is allready running. Also i want to ask if someone has at good ide on how to raise the running application when trying to start the second.

Header:


#ifndef APPLICATION_HH
#define APPLICATION_HH 1

#include <QtGui/QApplication>

class QSharedMemory;

class Application:public QApplication{

Q_OBJECT

public:
Application(int &argc, char **argv);
~Application();

bool lock();

private:
QSharedMemory *_singular;
};

#endif //APPLICATION_HH

Source:

#include <QtCore/QSharedMemory>

#include "Application.hh"

Application::Application(int &argc, char **argv):QApplication(argc, argv, true)
{
_singular = new QSharedMemory("MyVeryUniqueName", this);
}

Application::~Application()
{
if(_singular->isAttached())
_singular->detach();
}

bool Application::lock()
{
if(_singular->attach(QSharedMemory::ReadOnly)){
_singular->detach();
return false;
}

if(_singular->create(1))
return true;

return false;
}

Usage:

Application app(argc, argv);
if(!app.lock()){
QMessageBox::critical(0, "Error", "Application allready running");
exit(1);
}

Lykurg
20th March 2009, 12:18
You may want have a look here: SingleApplication (http://wiki.qtcentre.org/index.php?title=SingleApplication)
With the above mentioned you can send a message and react on it, e.g. show and process something...

nicolas1
20th March 2009, 13:12
you can create advisory-lock file and check its status in main().

BadKnees
20th March 2009, 13:43
Allright! Thanks.

Exactly what is was looking for:)

lni
20th March 2009, 15:53
I thought you can only have 1 instance of QApplication? That instance is stored in qApp as global vairable? Do I misunderstand you?

So, do

if ( qApp != NULL ) {
// application has been created
} else {
// create an application
}

lni
20th March 2009, 16:08
I thought you can only have 1 instance of QApplication? That instance is stored in qApp as global vairable? Do I misunderstand you?

So, do

if ( qApp != NULL ) {
// application has been created
} else {
// create an application
}

After reading the page at http://doc.trolltech.com/solutions/4/qtsingleapplication/index.html, I understand what it is. But why I don't have QtSingleApplication in my package?

I think it is very useful class. Is it possible to detect a running application in different machine? I am currently having several processes across different machines writing to same files, and I have to create/delete a file.lock to prevent the processes writing to the same file at the same time...

Lykurg
20th March 2009, 16:40
But why I don't have QtSingleApplication in my package?
It's only for commercial use: http://www.qtsoftware.com/products/appdev/add-on-products/catalog/4/Utilities/qtsingleapplication/ :crying:

cw9000
16th August 2012, 19:26
You may want have a look here: SingleApplication (http://wiki.qtcentre.org/index.php?title=SingleApplication)
With the above mentioned you can send a message and react on it, e.g. show and process something...

Just so everybody who uses this implementation using SingleApplication and more specifically the second one with QSharedMemory and QTimer...you have to modify the original code.
originally in the constructor it says

if (!sharedMemory.create(byteArray.size()))
you must change byteArray.size() to a number of bytes big enough to hold your message. The way it is right now, its only like 1 byte big. I had the darndest time finding out what was wrong.
Too bad it doesn't dynamically change in size. I set it to 30000, which is excessive, but I'm passing filenames and that can take up a lot of space.

But now, it works great. Thanks.

adrian.scarlat
4th November 2014, 15:13
You may want have a look here: SingleApplication (http://wiki.qtcentre.org/index.php?title=SingleApplication)
With the above mentioned you can send a message and react on it, e.g. show and process something...


Good day

I'm designing an application based on Qt 5 and one of the requirements is to have it allow only one application instance running.
I have used the code found at the link you provided and it works perfect. So I want to include it in my application.

My question is: does this code has a copyright and/or license information attached to it?

Thanks.