Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]How to prevent multiple instances of application
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]How to prevent multiple instances of application

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 19.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adnan
    wrote on 23 Jun 2012, 21:38 last edited by
    #1

    I want to allow only a single instance of my application and an attempt to run multiple instance should call a function from the running instance of the program. Is it possible in Qt.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on 23 Jun 2012, 22:10 last edited by
      #2

      QSharedMemory and QSystemSemaphore are your friends to pass IPC messages between multiple instances of an application. Just use a unique key for your application. If an instance can't create() a new QSharedMemory for that key, you know that another instance is already running. In that case the second instance can attach() to the existing QSharedMemory and write some message to it - the "main" instance will then process the message. The QSystemSemaphore is required to synchronize the read/write operations on the shared memory. I use one read and one write semaphore to implement a simple FIFO pipe in the shared memory.

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on 24 Jun 2012, 09:12 last edited by
        #3

        http://qt.gitorious.org/qt-solutions/qt-solutions/trees/master/qtsingleapplication

        1 Reply Last reply
        0
        • A Offline
          A Offline
          adnan
          wrote on 24 Jun 2012, 16:27 last edited by
          #4

          QtSingleApplication class does exactly what i want, i am surprised why it is not included in the Qt SDK. The examples are running fine, but when i try the same with my own application, the project compiles but does not run, it simply crashes without a clue!

          The following code works fine:
          @#include <QtGui/QApplication>

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

          QApplication a(argc, argv);
          manager w;
          if(argc==2)
          {
              w.durl=argv[1];
              w.on_actionAdd_Url_triggered();
          }
          w.show();
          return a.exec&#40;&#41;;
          

          }@

          But when i use QtSingleApplication class the application crashes without starting

          @#include <QtGui/QApplication>
          #include "manager.h"

          int main(int argc, char *argv[])
          {
          QtSingleApplication a(argc, argv);
          if (a.isRunning())
          return 0;
          manager w;
          if(argc==2)
          {
          w.durl=argv[1];
          w.on_actionAdd_Url_triggered();
          }
          w.show();
          a.setActivationWindow(&w);

          return a.exec(&#41;;
          

          }@

          The debugger points to the line
          @manager w;@

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adnan
            wrote on 24 Jun 2012, 17:57 last edited by
            #5

            It worked! thanks a lot. There was a problem in my code

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on 24 Jun 2012, 19:27 last edited by
              #6

              You're welcome. Please feel free to prepend your initial title with [Solved] to indicate that there is a solution inside.

              1 Reply Last reply
              0

              1/6

              23 Jun 2012, 21:38

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved