Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing Multiple Copies Running 2

Status
Not open for further replies.

FoxEgg

Programmer
Mar 24, 2002
749
AU
Running under W2000 Pro and WXP Pro ...when the app is running and the screen saver kicks in, the app is minimized.... sometimes my secretaries do not see the minimized version and re-open the app again ... and so on....and so on....

Needless to say this consumes a lot of system resources...

Is there any way to prevent this or should I post this in the XP Forum...

Thanks..

John Fox
 
Here's a little routine I use. Near the beginning of your main prg, add this code:
Code:
*......... check to see if app is already running .........
*
STORE 0 TO chk, tryit
IF FILE('C:\MyProg.zzz')
   tryit = FOPEN('C:\MyProg.zzz', 2)
   IF tryit < 0
      DO quitnow
      RETURN
   ELSE
      =FCLOSE(tryit)
      ERASE C:\MyProg.zzz
   ENDIF ( tryit < 0 )
ELSE
   chk = FCREATE('c:\MyProg.zzz')
ENDIF ( FILE('C:\MyProg.zzz') )
In your shutdown routine, or near the end of your app where it exits or quits, add this code:

=FCLOSE(chk)
ERASE C:\player.zzz


Then, in your procedure file or main prg, add this procedure:
Code:
*........................................................
PROCEDURE quitnow
   WAIT WINDOW &quot;Player Club program is already running! &quot;+;
      &quot;Press a key to exit.&quot;
   POP MENU _msysmenu
   SET TYPEAHEAD TO 20
   MODIFY WINDOW SCREEN ;
      TITLE &quot;Foxxxxxxxxxx&quot;;
      FONT 'Arial', 8;
      COLOR RGB(,,,,,,,);
      NOGROW;
      NOZOOM;
      nominimize
   MOVE WINDOW SCREEN CENTER
   CLEAR
   
   RETURN

This will open a 0 byte file (MyProg.ZZZ) exclusively, and keep it open as long as the app is running.
Dave S.
[cheers]
 
Thanks Guys.... I'll play with them this weekend ....

JF
 
Maybe I'm oversimplifying things but this works for me.
It relies on the fact that foxpro uses foxuser.dbf each time it fires up and keeps it loaded, therefore, logically, you can't &quot;use&quot; it again &quot;exclusively&quot; without generating an error.

*----- Check if Foxpro is already running
on error do starterr with ERROR()
set resource off
use foxuser exclusive
close databases
on error
set resource on
* Rest of app

*-------------------------------------------------
procedure starterr
*
PARAMETER MERROR

define window shutdown from 7,8 to 17,71 double
activate window shutdown
@ 1,1 say padc(&quot; FOXPRO MAY ALREADY BE RUNNING!!! &quot;,60,&quot;*&quot;) color gr+*/b
@ 3,1 say padc(&quot;The system reports error number &quot;+alltrim(transform(MERROR,&quot;9999&quot;)),60)
@ 5,1 say padc(&quot;Ring support if this continues&quot;,60)

wait window &quot;Press any key to quit.&quot;

release window shutdown
close databases
quit
return

Ken F
 
Provided you are using a foxuser.dbf for the app in the first place.
Dave S.
[cheers]
 
The generic ideas are all the same. So to add my bit....

&quot;Minimised...&quot;
Some times it is unintentional. When a session is unused, it gets minimised. This can be handled if you don't have a screen saver.

&quot;Check.zzz&quot; ..
I prefer it outside the FoxPo app!. I would prefer it in a batch file. No issues about exclusive, FoxUser etc.

HTH.
 
Thanks all.. got some time this weekend.. An Update...

I worked through the examples... but I experienced problems with all of them (which means that EITHER they are all wrong OR I am. and you know the answer to that conundrum!!!)

I WILL find out what I am doing wrong....

I am running my app from a batch file using its .prg extension///

I still seem to be able to defy logic and open multiple copies.....

Thanks for your help//// for the life of me I cannot understand why my app is being so bloody minded.

I'll do some more work and keep y'all posted..

JF
 
I did a little tweaking. Try this:
Code:
*......... check to see if app is already running .........
*
STORE 0 TO chk, tryit
IF FILE('C:\MyProg.zzz')               &&... If it's already there, 
   tryit = FOPEN('C:\MyProg.zzz', 2)   &&... try and open it
   IF tryit < 0                        &&... If an error, it's already open
      DO quitnow
      RETURN
   ENDIF ( tryit < 0 )
ENDIF ( FILE('C:\MyProg.zzz') )
chk = FCREATE('c:\MyProg.zzz')         &&... If it isn't there, put it there 

.
.
.
other code
.
.

In your shutdown routine, or near the end of your app 
where it exits or quits, add this code:

=FCLOSE(chk)
ERASE C:\MyProg.zzz


*Then, in your procedure file or main prg, add this procedure:
*........................................................
PROCEDURE quitnow
   WAIT WINDOW &quot;Player Club program is already running! &quot;+;
      &quot;Press a key to exit.&quot;
   POP MENU _msysmenu
   SET TYPEAHEAD TO 20
   MODIFY WINDOW SCREEN ;
      TITLE &quot;Foxxxxxxxxxx&quot;;
      FONT 'Arial', 8;
      COLOR RGB(,,,,,,,);
      NOGROW;
      NOZOOM;
      nominimize
   MOVE WINDOW SCREEN CENTER
   CLEAR
   
   RETURN
Dave S.
[cheers]
 
Hope that this helps

I had the very same problem and I solved creating a dummy database in the C:\ (just to make sure is local)
then I open the table exclusively (if I can't I close)

sounds silly, but it worked perfect!!!

 
Simplicity itself ! Thanks ... (April 1st noted with caution !)

JF
 
Please note my previous posts whereby I said ....

&quot;I worked through the examples... but I experienced problems with all of them (which means that EITHER they are all wrong OR I am. and you know the answer to that conundrum!!!)

Well I thought that DARKKESTREL has such a simple solution that it just HAD to work ... It didnt... even with exclusive on ... I was still able to run another copy !!!

So I am the problem... I shall delve further///

Is this because I am loading FOXPRO from a batch command ???? I am unsure... is it a Win 2000 thing ?

I'll search further,,,,

JF
 
It's probably that fetid Sydney air that's doing it.
John, I start from a batch file. Same batch file for all Windows versions,
@echo off
cls
cd \tilltek
foxprox -cc:\tilltek\config.fp -t a
cd cls
Then, first thing, I run that bit of script you see above.
Works fine.

All the other methods I looked at and tried rely on you making a file which you then check for. Problems arise when the system goes down, leaving that signal file behind.

As far as I can see, the only reason not to use foxuser.dbf would be because you don't want to record anything in it. If that's the case just mark it read only. If you use some other resources file, then use that as the check.

Ken F (in sunny Brisbane)
 
when the system goes down, leaving that signal file behind.

That's the reason behind the code:
Code:
IF FILE('C:\MyProg.zzz')               &&... If it's already there, 
   tryit = FOPEN('C:\MyProg.zzz', 2)   &&... try and open it
   IF tryit < 0                        &&... If an error, it's already open
If the app left it behind, try to open it. If another instance of the app has it open, the current one can't open it.
Dave S.
[cheers]
 
OK I am back... (sorry for the synaptic delay)...

Dave S...

Can I ask where does your routine ACTUALLY exit or quit when the second copy is attempted to be instantiated ? Eg in Procedure Quitnow... the final command is RETURN.. I cannot see a QUIT anywhere...

What am I missing here ?


PROCEDURE quitnow
WAIT WINDOW &quot;Player Club program is already running! &quot;+;
&quot;Press a key to exit.&quot;
POP MENU _msysmenu
SET TYPEAHEAD TO 20
MODIFY WINDOW SCREEN ;
TITLE &quot;Foxxxxxxxxxx&quot;;
FONT 'Arial', 8;
COLOR RGB(,,,,,,,);
NOGROW;
NOZOOM;
nominimize
MOVE WINDOW SCREEN CENTER
CLEAR

RETURN
 
Well, it has been a while.

I usually just use return, because when in the IDE, and I terminate execution of the app, I don't want to get dumped completely out of FoxPro. If you use RETURN in an executable, the executable won't have anywhere to return to, so it terminates the same as if you had used QUIT.

Here is another option:
Code:
IF 'MY_EXE.EXE' $ SYS(16)  &amp;&amp;... executable not command line
   QUIT
ELSE
   *... add your shut down / environment reset code here
   ZOOM WINDOW SCREEN NORMAL
   RETURN
ENDIF ( 'MY_EXE.EXE' $ SYS(16) )
SYS(16) is the currently execution procedure. So if it is not a .EXE, it will just return. If it is, it quits.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Yeah ! sorry about the 1 year hiatus.... As you can read I experienced some 'difficulties'.. so moved on to my VFP conversion... Then got busy at work.

Thanks for the explanation... and it explains a lot.

#1 I am a bit primitive... I run my app from a batch file using its .prg extension...

eg foxpro program.prg

rather than running a .exe

I suspect that causes certain problems BECAUSE fox is already running... therefore all the foxuser files are already active BEFORE the app executes the first time... so drops out...

#2 So I susspect that the

chk = FCREATE('c:\MyProg.zzz')

option will work.. but runs the risk of corrupt file etc crashed system etc

I'll keep working.

John



 
You may want to disable the FOXUSER files from being used in your app.
If you haven't already created a config file, create one.
Name it CONFIG.FP (if Foxpro DOS) or CONFIG.FPW (if Foxpro Windows).
Add a line that says:
RESOURCE=OFF

You may also want to add
EXCLUSIVE=OFF

That way, you won't have a FOXUSER.* collision, and the other files will be open shared.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
They say if you keep bashing your head on a wall you will eventually get the picture....

I NOW HAVE WHAT I WANT,,,, MAYBE IT WILL ASSIST OTHERS TOO,,,

Thanks to everyone for their Tips... Thanks Dave for that last one as well as the others...

Tilltek... (Queenslander and affected by too much sun) is doing something similar to what want I do..(ie running from a BATCH FILE) ... but I then thought that I should keep the whole thing OUT OF FOXPRO... I use a BATCH FILE TO START so why not test in the BATCH file... so I confess that the other day I posted it on the Win 98 forum and ...

thread615-784461 Thanks entirely to John R Barnett

posted code which does EXACTLY what all you guys have told me all along...

TEST TO SEE IF A DUMMY o Byte FILE exists..
CREATE it if not.. and runn app
Exit if it does...

I have copied that snippet and it means that I don't have to run FOXPRO to see if another instance is running... which makes sense to me.


******CODE

@echo off
IF EXIST %TEMP%\FoxEgg.txt GOTO END
' create zero byte lock file so it won't run
TYPE NUL &gt; %TEMP%\FoxEgg.TXT
' run the app
START &quot;C:\Program Files\Appname\Executable.exe&quot;
' delete the lock file to let it run
DEL %TEMP%\FoxEgg.txt
:END

***************

Thanks to you all...

I would be interested in Tilltekster's opinion on the thread615-784461

&quot;All the other methods I looked at and tried rely on you making a file which you then check for. Problems arise when the system goes down, leaving that signal file behind.&quot;

I have suggested that the dummy file be deleted in the reboot startup &quot;Autoexec.bat&quot; or what ever that file is now called in XP to solve that ....

John Fox
(In fetid Sydney Air)
 
Many good thoughts.

I use 2 solutions to solve this problem

1. Is the similar to the batch file. But I also have an other batch file to clear(delete) the locks. When the user can not run the program because the system was shut down improperly and left the file behind, then the user will click on the second batch to delete the lock file. Then execute the program as normal.

2. Now I use mostly this solution.

create a dbf file with one record in it called single.dbf
in the beginning of the program

use single shared &amp;&amp; However your style of programming is
if .not. rlock()
quit &amp;&amp; or return depends on your choice
endif

Nasib Kalsi
Try it.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top