如何通过 QsystemSemphore 强制我的应用程序成为单一进程? Qt、Linux

发布于 2024-09-30 17:49:16 字数 304 浏览 2 评论 0原文

可能的重复:
如何强制我的应用程序打开一个只有exe吗? qt、linux

你好,

我想强制我的应用程序只打开一个exe文件,如何通过QsystemSemaphore来做到这一点? 即如果过程是

10 倍!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期情话 2024-10-07 17:49:16

我认为使用 QSystemSemaphore 不容易解决这个问题。据我所知,此类仅支持阻止锁定尝试。这似乎是这里的要点:你只能调用release(总是成功,但不会告诉你任何东西)或acquire(它只会告诉你你永远阻止你达到了极限):

如果一个实例创建了一个信号量,它不知道它是否真的创建了它或者是否使用了现有的。如果它是一个二进制信号量,则可能会发生两件事。它要么获得锁,这意味着它是第一个也是唯一的实例,要么它只是阻塞在那里,直到第一个实例退出。

为了绕过这个问题,你可以将该测试放入一个单独的线程中,这样你就可以在该线程上使用外部超时来检查它是否被阻止,但是诚实地尝试这样的做法是肮脏的且极其危险的,没有办法让它工作 100 % 这样也安全。

当您谈论 exe 时,可能会认为这可以仅在 Windows 平台上解决?

//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;

对于更便携的解决方案,可以使用锁定文件来完成非常类似的事情(打开一个临时文件以进行独占访问,我认为这就是 QtSingleApplication 所做的),但我个人不喜欢那种基于文件的解决方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文