Thread: Stop program from running twice?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    Stop program from running twice?

    Hi,
    I just wanted to ask a quick question, although I have a feeling the answer isn't going to be nearly as short:
    How can you find out whether a program is running or not? That is, I want to prevent the program from running more than once at the same time.

    I don't mind using any system-specific API (I have a feeling I'll need to anyway). I need it to work on Windows (XP).

    Thanks
    I might not be a pro, but I'm usually right

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    If it's your application, you can create a semaphore and when another instance of the program tries to start, it can't, as it tries to create the same semaphore.

    If you want to check if process "x.exe" is running, then you can just list the processes using the toolhelp32 API and see if it's running.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try to create/open a file with a particular name in a "known" directory.

    The question of course is: Why would you want to do this [we had almost the same question a little while ago]?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I don't really understand what you mean matsp, but I want to do this to because I write and overwrite a file at a specific time in the program (it is supposed to run for some time). If I have the program running twice at the same time the file will get messed up and so will the program...
    I might not be a pro, but I'm usually right

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In that case, open the file with sharing attributes such that no other application can open the file for writes. Automatically, that would mean a lock against any other copy of that application, or other application that may wish to write to the same file, from running at that time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    But I don't have the file open all the time. I only open it when I need it, then close it when I'm finished. I don't think your solution would fix this up ...
    I might not be a pro, but I'm usually right

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use Mutex for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, create ANOTHER file that you DO hold open all the time [but my question would be WHY you are not holding the file open when you need to update it - or are you saying that your application runs for say half an hour, but only the last 2 minutes is outputting the data to some file?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Quote Originally Posted by Abda92 View Post
    Hi,
    I just wanted to ask a quick question, although I have a feeling the answer isn't going to be nearly as short:
    How can you find out whether a program is running or not? That is, I want to prevent the program from running more than once at the same time.

    I don't mind using any system-specific API (I have a feeling I'll need to anyway). I need it to work on Windows (XP).

    Thanks
    I hope this code gives a tip:

    Shared Sub Main(ByVal CmdArgs() As String)
    Dim ThisProcess As Process = Process.GetCurrentProcess()
    Dim AllProcesses As Process() =
    Process.GetProcessesByName(ThisProcess.ProcessName )
    If (AllProcesses.Length > 1) Then
    MessageBox.Show(ThisProcess.ProcessName + " is already
    running.", ThisProcess.ProcessName, MessageBoxButtons.OK,
    MessageBoxIcon.Error)
    Else
    Application.Run(New frmMain)
    End If
    End Sub

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So if you rename the executable, it would all be fine, yeah? And prevented to run any other app by the same name, but with different purpose? Seems like a bad way to solve this particular problem, to me.

    Since Abda92 wants to protect a file, it makes more sense to use a more secure and distinct way to lock other processes out. A lock-file [should be a "googleable" concept] would be a way that would work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Quote Originally Posted by matsp View Post
    So if you rename the executable, it would all be fine, yeah? And prevented to run any other app by the same name, but with different purpose? Seems like a bad way to solve this particular problem, to me.

    Since Abda92 wants to protect a file, it makes more sense to use a more secure and distinct way to lock other processes out. A lock-file [should be a "googleable" concept] would be a way that would work.

    --
    Mats
    How can you find out whether a program is running or not? That is, I want to prevent the program from running more than once at the same time.
    The Vb code helps him to what he wants

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It helps with the first, and sort of with the second, but as I pointed out, it has a few flaws.

    When helping someone it is often better to understand more of the problem than to solve the immediate problem.

    Quite often we get detailed questions here. They are not ALWAYS the "right" detailed question, so examining the reason why will often help solve the bigger picture problem.

    In this case, it is to prevent two instances using the same output file. [1]
    The right solution to that problem is either a mutex or a file with sharing set such that two apps can't open that file at once. This solves several other problems:
    1. The name of the application itself is not important any longer.
    2. Another app "in the same family" can use the same lock file to prevent another application from using the output file "at the wrong time" [e.g a display applicaiton that displays the output file].

    [1] The other question is, which I should have asked earlier: Why not make the output file a parameter or such - that way, multiple copies of the application can run at the same time. Using a non-sharable mode to open the file would then help prevent accidents where the same file is used for output. But there is no longer an implicit rule that forbitd two copies of the application from running at the same time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Abda92 View Post
    But I don't have the file open all the time. I only open it when I need it, then close it when I'm finished. I don't think your solution would fix this up ...
    If you want exclusive access to the file, then what's wrong with keeping it open all the time? That's how you maintain exclusive access.

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    42
    For file based locking:

    1. use a seperate file to control locking! Not the same file used for I/O.
    2. See fcntl (fd, F_SETLK, flock)
    3. use type = F_WRLCK

    Thus the set lock will work for one and only 1 process.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    42
    Quote Originally Posted by pCBoard.Member View Post
    Process.GetProcessesByName(ThisProcess.ProcessName )
    If (AllProcesses.Length > 1) Then
    This is seriously flawed.
    What happens when a program file has a soft link? or copied?

    Best to use file lock, Semaphore, mutex. Basically the issue is the name is not guaranteed to be distinct. Where if you code in a locking mechanism you can control the naming convention by not using the programs execution name but say a constant_name that represents the program logically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stop Program
    By peckitt99 in forum Windows Programming
    Replies: 6
    Last Post: 11-13-2007, 10:22 AM
  2. Running a program
    By BlueVoid in forum C Programming
    Replies: 7
    Last Post: 04-30-2005, 11:48 PM
  3. Determaining if a program is running
    By WebmasterMattD in forum Linux Programming
    Replies: 4
    Last Post: 04-09-2002, 04:36 PM
  4. Running program on background?
    By Couhilin in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2001, 07:50 AM
  5. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM