1. Two instances
- Posted by DonCole Dec 19, 2012
- 1199 views
Hello everybody,
What's the code to prevent 2 instances of the same program from running at the same time?
If you double click it or something like that.
Don Cole
2. Re: Two instances
- Posted by fizzpopsoft Dec 20, 2012
- 1151 views
Hi Don, The simple way would be to open a explicitly named file, and hold it open.
The second program, when attempting the same would get a -1 return from the open()
Regards,
Alan
3. Re: Two instances
- Posted by DonCole Dec 23, 2012
- 1131 views
Hi Don, The simple way would be to open a explicitly named file, and hold it open.
The second program, when attempting the same would get a -1 return from the open()
Regards,
Alan
Thank you Alan,
How long would I keep it open?
I know until the second file has attempted to open.
When I try to open two it is usally a mistake.
And I may not aware it.
Don Cole
4. Re: Two instances
- Posted by ghaberek (admin) Dec 24, 2012
- 1122 views
How long would I keep it open?
I know until the second file has attempted to open.
When I try to open two it is usally a mistake.
And I may not aware it.
Don,
PID files are probably the most universal way to ensure a single instance of your application. Here is a good StackOverflow post on the topic: Reference for proper handling of PID file on Unix. On Windows, you could use the TEMP path (e.g. getenv("TEMP")) in place of /var/run. If you are using Win32Lib, a call to setAppName() will fail if an application with that name is already running.
Here is a quick example for using PID files.
include "std/error.e" include "std/filesys.e" include "std/io.e" -- get the proper path for storing pid files ifdef WINDOWS then constant TEMP = getenv( "TEMP" ) elsedef constant TEMP = "/var/run" -- you could also use /tmp or maybe ~/.var/run end ifdef -- build the path to our pid file sequence pid_path = join_path({ TEMP, "myapp.pid" }) -- open the pid file integer pid_file = open( pid_path, "w" ) if pid_file = -1 then crash( "Unable to create PID file!" ) end if -- lock the pid file if not lock_file( pid_file, LOCK_EXCLUSIVE ) then close( pid_file ) crash( "Unable to lock PID file!" ) end if -- write this instance id (actual pid on Linux) atom this_pid = instance() printf( pid_file, "%d\n", {this_pid} ) -- flush and unlock the file flush( pid_file ) unlock_file( pid_file ) -- do a bunch of stuff here before exiting -- close and delete the pid file close( pid_file ) delete_file( pid_path )
-Greg
5. Re: Two instances
- Posted by fizzpopsoft Dec 25, 2012
- 1097 views
Hi Don,
if the PID solution is not what you want, you could have your program search for itself/others using pstools.
Availible from http://technet.microsoft.com/en-us/sysinternals/bb896649.aspx
extract of the doc from the "pslist" utility, which is part of pstools:
pslist exp
would show statistics for all the processes that start with "exp", which would include Explorer.
-d Show thread detail.
-m Show memory detail.
-x Show processes, memory information and threads.
-t Show process tree.
-s [n] Run in task-manager mode, for optional seconds specified. Press Escape to abort.
-r n Task-manager mode refresh rate in seconds (default is 1).
name Show information about processes that begin with the name specified.
-e Exact match the process name.
pslist is a command line utility, so you would need to issue the command and grab the output.
Of course, I am assuming your program is windows based.
Hope this helps!
Regards,
Alan