A one instance

For general discussion related FlowStone
Shoo
Posts: 33
Joined: Fri Sep 02, 2011 8:37 am
Location: Russia

A one instance

Post by Shoo »

How to allow to run only one instance of a program?

Thanks.
Embedded
Posts: 143
Joined: Sat Oct 30, 2010 1:42 pm

Re: A one instance

Post 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
Shoo
Posts: 33
Joined: Fri Sep 02, 2011 8:37 am
Location: Russia

Re: A one instance

Post by Shoo »

Oh!.. I have a FlowStone 1.1.2((((
Can in it establish some flag to see that one instance is already running?
Embedded
Posts: 143
Joined: Sat Oct 30, 2010 1:42 pm

Re: A one instance

Post 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!
Shoo
Posts: 33
Joined: Fri Sep 02, 2011 8:37 am
Location: Russia

Re: A one instance

Post 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!
Shoo
Posts: 33
Joined: Fri Sep 02, 2011 8:37 am
Location: Russia

Re: A one instance

Post 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>) ?
Attachments
quits.jpg
quits.jpg (21.29 KiB) Viewed 28357 times
Embedded
Posts: 143
Joined: Sat Oct 30, 2010 1:42 pm

Re: A one instance

Post by Embedded »

The only way is to use the fullscreen mode.

Hence why it's better to do this the Ruby way.
Shoo
Posts: 33
Joined: Fri Sep 02, 2011 8:37 am
Location: Russia

Re: A one instance

Post by Shoo »

Is it possible to use as the flag a presence of signal on some port or device busy?
DSP
Posts: 150
Joined: Fri May 14, 2010 10:55 pm

Re: A one instance

Post 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.
Morph
Posts: 53
Joined: Tue Jul 13, 2010 1:59 pm

Re: A one instance

Post 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.
Post Reply