-4

Possible Duplicate:
How to force my application to open one exe only? qt, linux

Hi,

I want to force my application to open one exe only, how to do it by QsystemSemaphore? i.e. if the proce

10x!

Community
  • 1
  • 1
devdev
  • 211
  • 2
  • 9
  • Why do you need to use `QSystemSemaphore` ? Why not using `QSingleApplication` ? – Jérôme Nov 04 '10 at 10:25
  • I don't want to use this class. – devdev Nov 04 '10 at 10:43
  • 1
    -1 Hey friend, Why don't you accept or edit your existing question (very similar to this one) before starting a new one almost identical??? http://stackoverflow.com/questions/4087235/how-to-force-my-application-to-open-one-exe-only-qt-linux – Symbiosoft Nov 04 '10 at 11:41
  • sorry, but I want all users to see my question - and the tytle is not the same :-( – devdev Nov 04 '10 at 11:53
  • I updated my response to your original question with QSystemSemaphore instead of QSharedMemory, it's almost the same. Let me know if it works. – Symbiosoft Nov 04 '10 at 12:03
  • I don't know how to connect my application to GUID:-( – devdev Nov 04 '10 at 12:49

1 Answers1

0

I don't think thats easily solvable using a QSystemSemaphore. As far as I see, this class only supports blocking lock attempts. That seams to be the major point there: you can only call release(succeeds always but doesn't tell you anything) or acquire(which only tells you that you hit the limit by blocking you forever):

If a instance creates a semaphore it won't know if it really created it or if its using a existing one. If it's a binary semaphore at acquire two things might happen. Either it gets the lock, meaning its the first and only instance, or it just blocks there until the first instance quits.

To bypass that problem you might put that test into a separate thread, so you could check if it gets blocked using a external timeout at that thread, but honesty attempts like that are dirty and extreme risky, there is no way to get it working 100% safely that way too.

As you talk about a exe, might be assumed this could be solved windows platform-only?

//create a somewhat unique semaphore key, eg by hashing the application path
QString Key = QString("Local\\MyApp_%1").arg(qHash(QByteArray(argv[0])), 8, 16, QChar('0'));
HANDLE hMutex = CreateMutexW(NULL, FALSE, Key.utf16());
//NOTE: unlikely, but hMutex might be NULL, check for errors

//try to lock the mutex, but don't wait for it
if(WAIT_TIMEOUT == WaitForSingleObject(hMutex, 0))
{ //mutex is locked by another instance
 //TODO: handle that somehow
 return 0;
}

//TODO: place standard QT startup code here, for example
QApplication a(argc, argv);
QtMyApplication w;
w.show();
int iReturn = a.exec();

//release and close the mutex
ReleaseMutex(hMutex);
CloseHandle(hMutex);
return iReturn;

For more portable solutions something very similar can be done using a lock-file(open a temporary file for exclusive access, I think thats what QtSingleApplication does), but I personally don't like that file-based workaround.

TheSUNSTAR
  • 101
  • 1
  • You could use QSharedMemory instead of a windows-mutex, create will return false if such a shared memory section already exists. – TheSUNSTAR Nov 04 '10 at 12:08