简体   繁体   English

如何检查 Qt GUI 应用程序是否已经在运行(在 Linux 中)?

[英]How to check if Qt GUI application is already running (in Linux)?

I need to check if my Qt GUI application is already running in the current X session .我需要检查我的 Qt GUI 应用程序是否已经在当前 X 会话中运行。 That is, I want to forbid running several instances of it.也就是说,我想禁止运行它的多个实例。 But running several instances by different users or by the same user on another (ie remote) X server should be allowed.但是应该允许不同用户或同一用户在另一个(即远程)X 服务器上运行多个实例。

How can this be done?如何才能做到这一点?

Qt has already a solution to single instance per user: QtSingleApplication . Qt 已经为每个用户提供了一个单一实例的解决方案: QtSingleApplication Check if this suits you.检查这是否适合您。

Create a dotfile (say, ~/.yourapp-xsessionID) when launching the program, and delete it when exiting;在启动程序时创建一个点文件(比如,~/.yourapp-xsessionID),并在退出时删除它; it the file already exists (ie it's already running) print out an error message and exit.如果文件已经存在(即它已经在运行)打印出错误信息并退出。

For the problems with this approach, see Firefox.有关此方法的问题,请参阅 Firefox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM