Results 1 to 5 of 5

Thread: ensure that only one instance of program is running

  1. #1
    Join Date
    Nov 2010
    Location
    iran/isfahan
    Posts
    13
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default ensure that only one instance of program is running

    Hi
    In many cases you may want to ensure that only one instance of your program is running. in another word,if an instance of program is running and the user tries to run a new instance,you want to found that and exit second program and maybe show an appropriate message. my question is how to implement this.
    I thought about some ways like register a key in windows registry or any other thing that could use as a flag that indicate that program is running or not( by setting this flag when program runs and unset it on exiting the program) but it's a bad idea when the user kills the process or program exit with any bad status.

    if you know the usual way to implement that feature, please help me

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: ensure that only one instance of program is running

    One cross platform technique but flawed in some contexes.

    Create a file with the PID of the program.

    1. check if this file exists.
    If it does, check the PID, is it running or not? If it is running, close the new application.
    If for some reason the file didn't get erased, the PID will most likely not be running (a flaw) and the file can be updated with the new PID.

    2. When the program starts, write the PID to a file.

    3. When the program exits, remove that file.

    Or set an environment variable.
    Last edited by tbscope; 5th January 2011 at 19:26.

  3. The following user says thank you to tbscope for this useful post:

    alireza.mirian (5th January 2011)

  4. #3
    Join Date
    Nov 2010
    Location
    iran/isfahan
    Posts
    13
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: ensure that only one instance of program is running

    Quote Originally Posted by tbscope View Post
    One cross platform technique but flawed in some contexes.

    Create a file with the PID of the program.

    1. check if this file exists.
    If it does, check the PID, is it running or not? If it is running, close the new application.
    If for some reason the file didn't get erased, the PID will most likely not be running (a flaw) and the file can be updated with the new PID.

    2. When the program starts, write the PID to a file.

    3. When the program exits, remove that file.

    Or set an environment variable.
    thanks alot. this is like my "flag" solution and checking PID that U suggest will solve the problem of my solution. this solve the problem but my question is that, is this the formal and usual way to solve this? I ask it because it seems a little unsecure. if anyone removes the file, he/she can have more than one instance of program and if its critical to not have more than one instance, it may make mistake.

  5. #4
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    2
    Thanked 6 Times in 6 Posts

  6. The following user says thank you to javimoya for this useful post:

    alireza.mirian (5th January 2011)

  7. #5
    Join Date
    Nov 2010
    Location
    iran/isfahan
    Posts
    13
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: ensure that only one instance of program is running

    wow, that was EXACTLY what i want! thank U very very much
    but when I tried it, complier says:"Cannot open include file:'QtSingleApplication':No such file or directory"

    Quote Originally Posted by alireza.mirian View Post
    wow, that was EXACTLY what i want! thank U very very much
    but when I tried it, complier says:"Cannot open include file:'QtSingleApplication':No such file or directory"
    got it, got it... thank U

Similar Threads

  1. running external console program by gui program
    By alireza.mixedreality in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2010, 19:05
  2. Replies: 5
    Last Post: 9th June 2008, 12:21
  3. Replies: 5
    Last Post: 5th July 2007, 21:49
  4. Replies: 1
    Last Post: 17th May 2006, 01:23
  5. Replies: 5
    Last Post: 16th January 2006, 06:15

Tags for this Thread

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.