Results 1 to 6 of 6

Thread: How to detect that first instance of application is already running?

  1. #1
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question How to detect that first instance of application is already running?

    Hello,
    i'd like to know if there is a Qt way to detect if one instance of application is already running in memory?
    What i'm trying to do :
    If user launches my app it should check if it is already in memory (first copy) and if it is, than show a main window of first copy.
    I'm on WinXP with Qt 4.4.0
    Is there some nifty Qt class or method for that?
    Actually i have a solution with a thread and socket listening to local port, but it's not qt.
    So, Qt way of doing this?

  2. #2
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to detect that first instance of application is already running?

    http://www.qtcentre.org/forum/showth...detect+running

    seems that is a Qt way - QtSingleApplication, but it's in solution area and not free.
    May be Qt architectors will move this needful class into main package in the future...
    So i'm on my own solution with locally started thread listening to socket.

  3. #3
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to detect that first instance of application is already running?


  4. #4
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to detect that first instance of application is already running?

    You can use Singleton pattern (if I'm not mistaken).
    I'm a rebel in the S.D.G.

  5. #5
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to detect that first instance of application is already running?

    Singleton Pattern is useful for creating just one instance of a class / object...

    Try :

    Qt Code:
    1. #pragma once
    2.  
    3. #ifndef LimitSingleInstance_H
    4. #define LimitSingleInstance_H
    5.  
    6. class CLimitSingleInstance
    7. {
    8. protected:
    9. DWORD m_dwLastError;
    10. HANDLE m_hMutex;
    11.  
    12. public:
    13. CLimitSingleInstance(TCHAR *strMutexName)
    14. {
    15. //Make sure that you use a name that is unique for this application otherwise
    16. //two apps may think they are the same if they are using same name for
    17. //3rd parm to CreateMutex
    18. m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
    19. m_dwLastError = GetLastError(); //save for use later...
    20. }
    21.  
    22. ~CLimitSingleInstance()
    23. {
    24. if (m_hMutex) //Do not forget to close handles.
    25. {
    26. CloseHandle(m_hMutex); //Do as late as possible.
    27. m_hMutex = NULL; //Good habit to be in.
    28. }
    29. }
    30.  
    31. BOOL IsAnotherInstanceRunning()
    32. {
    33. return (ERROR_ALREADY_EXISTS == m_dwLastError);
    34. }
    35. };
    36. #endif
    To copy to clipboard, switch view to plain text mode 

    In your main.cpp :

    Qt Code:
    1. CLimitSingleInstance g_SingleInstanceObj(TEXT("Global\\{B5D47D64-F5F9-46e3-AFB8-EC34D0F863F1}"));
    2.  
    3. if (g_SingleInstanceObj.IsAnotherInstanceRunning())
    4. {
    5. QMessageBox::critical(NULL, "Warning",
    6. "There is another instance of this application already running." );
    7. return FALSE;
    8. }
    To copy to clipboard, switch view to plain text mode 



    Regards,
    Steve

  6. #6
    Join Date
    Jun 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: How to detect that first instance of application is already running?

    Wow!
    Thanks, guys!

Similar Threads

  1. Knowing if application is running
    By mourad in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2008, 10:47
  2. Only let one instance of the application run
    By chrisdsimpson in forum Newbie
    Replies: 1
    Last Post: 9th April 2008, 21:30
  3. Replies: 10
    Last Post: 10th March 2008, 12:28
  4. Replies: 10
    Last Post: 11th June 2007, 09:18
  5. Replies: 5
    Last Post: 16th January 2006, 05:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.