Page 1 of 3
A one instance
Posted: Mon Jul 16, 2012 4:15 am
by Shoo
How to allow to run only one instance of a program?
Thanks.
Re: A one instance
Posted: Mon Jul 16, 2012 12:04 pm
by Embedded
One way would be to use the Ruby Module, and write some Ruby code to see if there is already an instance running.
Looking at some web posts it seems people open a file and lock it, if the file is already open then don't allow the 2nd instance (ie. close it straight away).
Here some Ruby Code I found that could be tweaked to do this I think:
Code: Select all
def log(msg)
output 0, "#{$$} #{msg}"
end
def lock
File.open("test.loc", File::RDWR|File::CREAT, 0644) {|f|
log "Attempting to lock..."
if (f.flock(File::LOCK_EX | File::LOCK_NB) == false)
log "Another instance is already running! Exiting"
abort
end
log "Lock acquired"
f.rewind
f.write($$)
f.flush
10.times do |i|
log "Doing work... #{i}"
sleep(1)
end
log "Exiting"
sleep(1)
}
end
def event i,v,t
lock
end
Re: A one instance
Posted: Tue Jul 17, 2012 8:38 am
by Shoo
Oh!.. I have a FlowStone 1.1.2((((
Can in it establish some flag to see that one instance is already running?
Re: A one instance
Posted: Tue Jul 17, 2012 10:01 am
by Embedded
You could write a text file to disk, if it exists then close the second instance. But you will have to manage your program exit to delete the file before exiting (so disable close on ESC and make your own close button using the EXE Quit module).
Also if for some reason your app. crashes and doesn't close formally the text file will still be there and then block you starting instance one!
So then you might consider writing a time stamp to the file on a regular basis like a watchdog, so if your app. bombs and you restart the time stamp is old and it allow your to start.
My advice is to Get FlowStone V2 and do it properly using Ruby!
Re: A one instance
Posted: Tue Jul 17, 2012 12:47 pm
by Shoo
Embedded wrote:My advice is to Get FlowStone V2
This decide my boss...
Embedded wrote:So then you might consider writing a time stamp to the file on a regular basis like a watchdog, so if your app. bombs and you restart the time stamp is old and it allow your to start.
It's a great idea!
Thank you very much!
Re: A one instance
Posted: Tue Jul 17, 2012 5:42 pm
by Shoo
Embedded wrote:you will have to manage your program exit to delete the file before exiting (so disable close on ESC and make your own close button using the EXE Quit module).
How hide (or disable) this elements (and the <Alt-F4>) ?
Re: A one instance
Posted: Tue Jul 17, 2012 11:31 pm
by Embedded
The only way is to use the fullscreen mode.
Hence why it's better to do this the Ruby way.
Re: A one instance
Posted: Wed Jul 18, 2012 4:43 am
by Shoo
Is it possible to use as the flag a presence of signal on some port or device busy?
Re: A one instance
Posted: Wed Jul 18, 2012 12:32 pm
by DSP
Another idea would be to setup inter application communication using UDP and the network modules.
So if you set up a network Server on a port (say 80) in your FlowStone application to listen for a second instance, when a second instance was started (Client) it would send out a message get a response then shut down.
Re: A one instance
Posted: Wed Jul 18, 2012 12:39 pm
by Morph
Embedded wrote:You could write a text file to disk, if it exists then close the second instance. But you will have to manage your program exit to delete the file before exiting (so disable close on ESC and make your own close button using the EXE Quit module).
Also if for some reason your app. crashes and doesn't close formally the text file will still be there and then block you starting instance one!
So then you might consider writing a time stamp to the file on a regular basis like a watchdog, so if your app. bombs and you restart the time stamp is old and it allow your to start.
The TaskList command can make a list of instances of a given program. Then compare if the lines of text exceeds more than that of one program then kill the process. I can build example if needed.