Re: Second instance
- Posted by DonCole Jan 07, 2016
- 1602 views
Yes, I would like more details on method 1.
Thank you,
Don Cole
Hello everyone,
Thank you, Don Cole
I've used 2 cross platform methods to do this in the past.
- Create a "lock.pid" file for your program
- 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)