1. Second instance

Hello everyone,

Does anyone know the code that will stop a second instant of a program form running. For example while I'm running one instance of myPrograme.exw. By mistake or some other reason a second instant runs. how can i stop it.

Thank you, Don Cole

new topic     » topic index » view message » categorize

2. Re: Second instance

DonCole said...

Hello everyone,

Does anyone know the code that will stop a second instant of a program form running. For example while I'm running one instance of myPrograme.exw. By mistake or some other reason a second instant runs. how can i stop it.

Thank you, Don Cole

I've used 2 cross platform methods to do this in the past.

  1. Create a "lock.pid" file for your program
  2. Bind to a socket

In the first case, you might choose a location relative the your executable program and create a file. You could perhaps call the file "lock.pid". When you start your program you can create the file and when your program terminates, delete the file. If the file already exists when you start your program, then exit. There are some details I'm leaving out of this response. If you are interested in pursuing this, ask and fill in more details.

The second solution involves binding to a tcp/ip socket when your program starts. If you can't bind, assume it's because another instance of your program is running. This is a very easy and simple solution but it assumes that the host OS and/or firewall will not be too noisy about the binding. It also assumes that your predetermined port is usually/always available. Anyway, I've used this solution when it was not reasonable to use option 1. (And actually, it's easier IMO)

-Xecronix

new topic     » goto parent     » topic index » view message » categorize

3. Re: Second instance

If you're using Win32Lib then you can use setAppName() which will return non-zero if the application name is already in use (i.e. running).

    if setAppName("Super Database") != 0 then 
        warnErr("Application is already running") 
    end if 

Another option would be to search the list of active processes running on the system for a PID matching your own executable path.

Unfortunately, Euphoria currently lacks a cross-platform library for working with enumerating and controlling processes. sad

-Greg

new topic     » goto parent     » topic index » view message » categorize

4. Re: Second instance

ghaberek said...

Unfortunately, Euphoria currently lacks a cross-platform library for

controlling processes.

Not true. There is std/pipeio.e

ghaberek said...

Unfortunately, Euphoria currently lacks a cross-platform library for

enumerating

processes.

True. Though this is really easy on most flavors of nix - just search the files in /proc

new topic     » goto parent     » topic index » view message » categorize

5. Re: Second instance

See Edita\src\easinst.ew for a windows-only solution.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Second instance

On Windows, I use Thomas Parslow's IPC library to do the job.

  if not ipc_RegisterProcessName("<program name>") then 
    puts(2, "<program name> is already running!\n") 
    abort(1) 
  end if 

Jean-Marc

new topic     » goto parent     » topic index » view message » categorize

7. Re: Second instance

jimcbrown said...
ghaberek said...

Unfortunately, Euphoria currently lacks a cross-platform library for

controlling processes.

Not true. There is std/pipeio.e

The Pipe I/O library is only useful if you simply want to start a process and then forget about it.

Until we can determine the status of the process we started, I don't see it as terribly useful.

This is the type of loop we'll need if we want Pipe I/O to be useful:

atom p = process:exec( "my command" ) 
 
while process:is_running( p ) do 
 
    sequence data = process:read( p ) 
    -- do stuff 
 
end while 
jimcbrown said...
ghaberek said...

Unfortunately, Euphoria currently lacks a cross-platform library for

enumerating

processes.

True. Though this is really easy on most flavors of nix - just search the files in /proc

Yes, but it's inherently more complicated on Windows, as we know many things there are.

I suppose get_process_list() is just something that needs to go on the "wishlist" of features for Euphoria.

-Greg

new topic     » goto parent     » topic index » view message » categorize

8. Re: Second instance

ghaberek said...
jimcbrown said...
ghaberek said...

Unfortunately, Euphoria currently lacks a cross-platform library for

controlling processes.

Not true. There is std/pipeio.e

The Pipe I/O library is only useful if you simply want to start a process and then forget about it.

Until we can determine the status of the process we started, I don't see it as terribly useful.

This is the type of loop we'll need if we want Pipe I/O to be useful:

atom p = process:exec( "my command" ) 
 
while process:is_running( p ) do 
 
    sequence data = process:read( p ) 
    -- do stuff 
 
end while 

I was under the impression that one could do this today:

 
constant ESRCH = 3 
 
object z = pipeio:create() 
object p = pipeio:exec( "my command", z ) 
 
while pipeio:get_errno() != ESRCH with entry do 
 
    sequence data = pipeio:read( p[STDOUT] ) 
    -- do stuff 
 
entry 
 
    -- check if process is still alive and set errno appropriately if not 
    pipeio:kill( p, 0 ) 
 
end while 
 
-- clean up by closing the pipes 
pipeio:kill( p ) 
ghaberek said...
jimcbrown said...
ghaberek said...

Unfortunately, Euphoria currently lacks a cross-platform library for

enumerating

processes.

True. Though this is really easy on most flavors of nix - just search the files in /proc

Yes, but it's inherently more complicated on Windows, as we know many things there are.

I suppose get_process_list() is just something that needs to go on the "wishlist" of features for Euphoria.

-Greg

No argument from me on this. It's probably simple enough that if someone had a patch, we could sneak it into 4.1.0 and 4.0.6

new topic     » goto parent     » topic index » view message » categorize

9. Re: Second instance

Yes, I would like more details on method 1.

Thank you,

Don Cole

xecronix said...
DonCole said...

Hello everyone,

Does anyone know the code that will stop a second instant of a program form running. For example while I'm running one instance of myPrograme.exw. By mistake or some other reason a second instant runs. how can i stop it.

Thank you, Don Cole

I've used 2 cross platform methods to do this in the past.

  1. Create a "lock.pid" file for your program
  2. Bind to a socket

In the first case, you might choose a location relative the your executable program and create a file. You could perhaps call the file "lock.pid". When you start your program you can create the file and when your program terminates, delete the file. If the file already exists when you start your program, then exit. There are some details I'm leaving out of this response. If you are interested in pursuing this, ask and fill in more details.

The second solution involves binding to a tcp/ip socket when your program starts. If you can't bind, assume it's because another instance of your program is running. This is a very easy and simple solution but it assumes that the host OS and/or firewall will not be too noisy about the binding. It also assumes that your predetermined port is usually/always available. Anyway, I've used this solution when it was not reasonable to use option 1. (And actually, it's easier IMO)

-Xecronix

new topic     » goto parent     » topic index » view message » categorize

10. Re: Second instance

ghaberek said...

I suppose get_process_list() is just something that needs to go on the "wishlist" of features for Euphoria.

Pete Stoner already wrote GetProcessList() for Windows in 2006. I used it, it works perfectly.

Jean-Marc

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu