Re: Second instance

new topic     » goto parent     » topic index » view thread      » older message » newer message
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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu