[Gambas-user] How to avoid second instance?

Eilert eilert-sprachen at ...221...
Thu Sep 16 08:58:38 CEST 2004


Hi Alberto,

this is my solution - it does not contain IsDir() yet because Ron's mail 
came too late :-)

There is some German in it:

datei = file
pfad  = path
dtnr  = file number
laeuft = is running

One could make the second "FOR EACH..." somewhat shorter by using 
IsDir(), but unfortunately there is no IsFile(), so the first "FOR 
EACH..." will have to stay I guess.

I've built it into one of my projects and it runs perfectly this way.

Rolf



PUBLIC SUB CheckProcId()
DIM datei$ AS String
DIM progr$ AS String
DIM pfad$  AS String
DIM dtnr   AS File
DIM t$     AS String
DIM laeuft AS Boolean

   laeuft = FALSE

   progr$ = CStr(Application.Id)

   pfad$ = "/home/" & System.User & "/"

   FOR EACH datei$ IN Dir(pfad$, ".msyb-lock")
     OPEN pfad$ & datei$ FOR READ AS #dtnr
       LINE INPUT #dtnr, t$
     CLOSE #dtnr
   NEXT

   IF t$ = "" THEN
     OPEN pfad$ & ".msyb-lock" FOR WRITE CREATE AS #dtnr
       PRINT #dtnr, progr$
     CLOSE #dtnr

   ELSE
     FOR EACH datei$ IN Dir("/proc/", t$)
       laeuft = TRUE
     NEXT

     IF laeuft THEN
       QUIT

     ELSE
       OPEN pfad$ & ".msyb-lock" FOR WRITE CREATE AS #dtnr
         PRINT #dtnr, progr$
       CLOSE #dtnr

     END IF

   END IF


END


Alberto Torres schrieb:
> Maybe using a lock file with the PID in it:
> 
> -Check if exists the lock file (say /tmp/yourapp.lock) and it has a 
> number in it.
>    -If so, check if the program with that PID is running (i.e. checking 
> /proc/[pid], say pid is 12345, check if directory /proc/12345 exists)
>       -If it is already running, exit
>       -Else, replace the lock file writng Application.Id in it, and 
> continue.
> 
> Regards
> DiThi
> 
> Eilert escribió:
> 
>> Hi,
>>
>> how can I avoid that a second instance of the same prog is started e. 
>> g. by a double click?
>>
>> Is there a way of finding out if there is another instance of the same 
>> prog by the same user running? Or do you think a file-based solution 
>> (lock files) is easier to implement?
>>
>> Rolf
>>





More information about the User mailing list