[Gambas-user] Avoiding multiple instances

Leonardo Miliani leonardo at ...1237...
Thu Jan 31 16:00:37 CET 2008


Jeffrey Cobb ha scritto:
> I've made a small toolbar app to run in fluxbox.  I've included the
> binary in my xinitrc file.  But I'd like to know how I can avoid
> multiple instances of the same app running.  How does a gambas program
> detect another copy of itself running?
> 
> Jeff
> 
> 
> 
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
> 

I can suggest you how I solved this problem.
When an application of mine starts, it creates a file just by executing
this code:

PRIVATE FUNCTION Check_If_Running() AS Boolean
DIM TempString AS String
DIM hFile AS File
  TempString = Application.Path & "/.running"
  IF Exist(TempString) THEN
    Message.Warning("No multiple instances allowed")
    RETURN TRUE
  END IF
  hFile = OPEN TempString FOR CREATE
  CLOSE #hFile
  RETURN FALSE
END

The first time that the app starts it creates an empty hidden file and
it returns FALSE because it didn't find another instance running.
The second time that you call the function you'll get TRUE because the
program is running (it created the check file).
Just by checking its existance you can know if another instance of the
program is running.

Of course, when you close the app, you have to delete it:

PUBLIC SUB Form_Close()

  IF Exist(Application.Path & "/.running") THEN
    KILL Application.Path & "/.running"
  END IF

END

-- 
Ciao.
Leo.

Web: www.leonardomiliani.com
E-mail: leonardo at ...1237...

Co-fondatore di Gambas-it.org: www.gambas-it.org
(il sito italiano dedicato alla comunità di Gambas)

Scegli software opensource - Choose opensource software




More information about the User mailing list