如何强制我的应用程序只打开一个exe? linux

发布于 2024-09-30 14:20:03 字数 112 浏览 4 评论 0原文

我希望我的应用程序仅打开一个进程,即如果该一个进程已打开并且用户想要再次打开 exe - 它不会打开另一个进程。

我怎样才能在 Qt - linux 中做到这一点?

10 倍!

I want my application to open only one process, i.e. if the one process is already opened and the user want to open the exe again - it won't open another process.

how can I do it in Qt - linux?

10x!

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

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

发布评论

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

评论(4

爱人如己 2024-10-07 14:20:03

您要查找的是 QtSingleApplication

如果您启动应用程序的另一个实例,第一个实例甚至会收到有关它的通知(您可以传递您想要的任何数据结构)。

每当另一个实例启动时,我就用它来将现有应用程序带到前面。

What you're looking for is QtSingleApplication.

If you start another instance of your application, the first one will even get notified about it (you can pass whatever data structure you want).

I used it to bring the existing application to the front whenever another instance is started.

时光是把杀猪刀 2024-10-07 14:20:03

在 main.cpp 中使用以下代码来防止运行应用程序的多个实例。我在 Linux 下(在 QtCreator 中)测试了这段代码,它可以工作(也适用于 Windows)。我发现这个解决方案简单且易于实施。该示例适用于控制台应用程序。 GUI 应用程序的代码保持不变,请检查代码中的注释。

//main.cpp
#include <QCoreApplication> //Console application
//#include <QApplication>     //GUI application
#include <QSharedMemory>
#include <QDebug>
//Your QMainWindow derivated class goes here :
//#include "MainWindow.h"

int main(int argc, char *argv[])
{

  QCoreApplication app( argc, argv );

  app.processEvents();

  //---- Check for another instance code snippet ----

  //GUID : Generated once for your application
  // you could get one GUID here: http://www.guidgenerator.com/online-guid-generator.aspx
  QSharedMemory shared("62d60669-bb94-4a94-88bb-b964890a7e04");

  if( !shared.create( 512, QSharedMemory::ReadWrite) )
  {
    // For a GUI application, replace this by :
    // QMessageBox msgBox;
    //msgBox.setText( QObject::tr("Can't start more than one instance of the application.") );
    //msgBox.setIcon( QMessageBox::Critical );
    //msgBox.exec();

    qWarning() << "Can't start more than one instance of the application.";

    exit(0);
  }
  else {
      qDebug() << "Application started successfully.";
  }
  //---- END OF Check for another instance code snippet ----

  // Only one instance is running, declare MainWindow
  //MainWindow myMainWindow;
  //myMainWindow.show();


  //We enter the Qt Event loop here, we don't leave until the MainWindow is closed
  //or the console application is terminated.
  return app.exec();
}

Use the following code in the main.cpp to prevent to run more than one instance of your application. I tested this code under Linux (in QtCreator) and it works (works for Windows too). I find this solution simple and easy to implement. The example is for a console application. The code remain the same for a GUI application, check the comments in the code.

//main.cpp
#include <QCoreApplication> //Console application
//#include <QApplication>     //GUI application
#include <QSharedMemory>
#include <QDebug>
//Your QMainWindow derivated class goes here :
//#include "MainWindow.h"

int main(int argc, char *argv[])
{

  QCoreApplication app( argc, argv );

  app.processEvents();

  //---- Check for another instance code snippet ----

  //GUID : Generated once for your application
  // you could get one GUID here: http://www.guidgenerator.com/online-guid-generator.aspx
  QSharedMemory shared("62d60669-bb94-4a94-88bb-b964890a7e04");

  if( !shared.create( 512, QSharedMemory::ReadWrite) )
  {
    // For a GUI application, replace this by :
    // QMessageBox msgBox;
    //msgBox.setText( QObject::tr("Can't start more than one instance of the application.") );
    //msgBox.setIcon( QMessageBox::Critical );
    //msgBox.exec();

    qWarning() << "Can't start more than one instance of the application.";

    exit(0);
  }
  else {
      qDebug() << "Application started successfully.";
  }
  //---- END OF Check for another instance code snippet ----

  // Only one instance is running, declare MainWindow
  //MainWindow myMainWindow;
  //myMainWindow.show();


  //We enter the Qt Event loop here, we don't leave until the MainWindow is closed
  //or the console application is terminated.
  return app.exec();
}
回忆躺在深渊里 2024-10-07 14:20:03

这可能与您无关,但我认为提出它会很有用。我自己正在使用 QtSingleApplication ,几天前经历了一些奇怪的行为。 QtSingleApplication 似乎并不在所有情况下都有效。我在 Windows 中进行了这种体验,但根据这是 Windows 特定的错误还是 QtSingleApplication 的设计意图,它也可能适用于 Linux。

根据您启动应用程序的方式,可能存在多个实例。当我使用安装程序测试我的应用程序时,我获得了这种体验。安装程序完成后自动启动应用程序。然后,当我使用桌面链接启动我的应用程序时,我有两个实例正在运行。因此,QtSingleApplication 的功能似乎取决于应用程序的启动方式(以及由哪个用户启动?)。文档对此并不清楚。但我认为,如果没有另外说明,人们通常会期望它在所有情况下都有效。

因此,如果您不需要 QtSingleApplication 添加的额外功能,则 QSystemSemaphore 或 QSharedMemory 似乎是更好的选择。

This may not concern you, but I thought it would be useful to bring it up. I'm using QtSingleApplication myself and experienced some odd behavior a few days ago. QtSingleApplication doesn't seem to work under all circumstances. I made this expierence in windows, but depending on wether this is a windows specific bug or intended by the design of QtSingleApplication, it may also apply to linux.

Depending on the way you start your application multiple instances are possible. I made this experience when I did a testdrive of my application using my installer. The installer automatically starts the application after finishing. When I then started my application using the desktop link, I had two instances running. So the functionality of QtSingleApplication seems to depend on the way how (and by which user?) the application is started. The documentation is unclear about this. But I think usually one would expect this to work under all circumstances, if not stated otherwise.

So, if you don't need the extra functionality added by QtSingleApplication, QSystemSemaphore or QSharedMemory seems to be the better way to go.

池予 2024-10-07 14:20:03

您的应用程序可以检查用户主目录中是否存在某个文件。如果存在,则应用程序退出。如果不存在,应用程序将创建它并继续。当然,如果用户一次多次启动应用程序,您可能会遇到竞争条件。但对于大多数情况,这个简单的解决方案应该足够了。

Your application could check if a certain file in the user's home directory is present. If it is present, the application exits. If it is not there, the application creates it and continues. Of course, you might get a race condition if the user starts the application several times at once. But for most cases this simple solution should be sufficient.

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