A one instance
Posted: Mon Jul 16, 2012 4:15 am
How to allow to run only one instance of a program?
Thanks.
Thanks.
DSP Robotics and FlowStone Graphical Programming Software Support and Forums
https://dsprobotics.com/support/
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
endThis decide my boss...Embedded wrote:My advice is to Get FlowStone V2
It's a great idea!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.
How hide (or disable) this elements (and the <Alt-F4>) ?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).
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.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.