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!

on shutdown command - not performing what I ask 4

Status
Not open for further replies.

ahaws

Programmer
Nov 28, 2001
355
US
Hi all-
Been having a problem with temp ".dbf" files remaining in my active directory when a program is prematurely shutdown due to user not exiting the program properly,power failure, etc.

I have a program that checks to see if a temp file exists, and if it exists, make sure it's closed, then delete it. I named this .prg "Closedbfs"

My ON SHUTDOWN command should kick in at these points but doesnt always delete my files. I dont' get it.

This is the code behind my "Quit" button on my form -

thisform.release()
DO clr_sets

SET SYSMENU TO DEFAULT
set talk off
CLEAR
DO QUITAPP

close all
close databases

release all
on shutdown



Then in my "main" driver program I have:

On SHUTDOWN
Do closedbfs
Clear Events
Close Databases All
Set Sysmenu To Default
Clear All
Close All


Am I issuing the ON SHUTDOWN command properly? Please - any help is appreciated.
Thanks
Angie
 
ahaws

The following is one way of doing it.

In your main.prg as you set up the application
Code:
[COLOR=blue]ON SHUTDOWN DO CloseApp[/color][COLOR=green]
* Code
* Code[/color][COLOR=blue]
DO FORM main NAME oMain LINKED
READ EVENTS
DO CloseApp[/color]

In the .Click() event of the command button
Code:
[COLOR=blue]THISFORM.Release()[/color]

In the .Destroy() event of the form
Code:
[COLOR=blue]CLEAR EVENTS[/color]

In the .CloseApp procedure in main.prg
Code:
[COLOR=blue]On SHUTDOWN [/color][COLOR=green]&& Avoid recursive call[/color][COLOR=blue]
DO clr_sets
SET SYSMENU TO DEFAULT
set talk off
CLEAR
close all
close databases
Do closedbfs
Clear All
Close All
[/color]



FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander[sup]tm[/sup].com
PDFcommander[sup]tm[/sup].co.uk


 
This does not solve your problem with the ON SHUTDOWN.
However, it is a function I wrote to deal with the temporary file situation you describe.

This function returns a filename that does not already exist in the form of ... TMP1000.DBF, TMP1001.DBF, TMP1002.DBF, etc ...

It also automatically deletes any files that have this same format (TMP????.DBF) and are dated prior to today's date.

This way I never have to worry about temporary files building up.

There are some potential problems with this but I have never had a problem with it. If it seems like it could work for you, feel free.


function tmpdbf
local cValue, cFilename, nValue, aTmpsLen, dFiledate, dToday
dToday = date()
* This routine erases TMP????.* files which were created prior to today
aDir(aTmps,"TMP????.*")
if type("aTmps") <> "U"
aTmpsLen = aLen(aTmps,1)
for j = 1 to aTmpsLen
cFilename = aTmps[j,1]
dFiledate = aTmps[j,3]
if dFiledate < dToday
erase (cFilename)
endif
endfor
endif
nValue = 1000
do while .t.
nValue = nValue + 1
cValue = str(nValue,4)
cFilename = "TMP"+cValue+".DBF"
if not file(cFilename)
exit
endif
enddo
return cFilename



Don


 
Angie,

Am I issuing the ON SHUTDOWN command properly?

No. The code in your main program will never be called.

You need to put something like this at the start of your main program:

ON SHUTDOWN DO ShutdownProc

Then write a procedure called ShutdownProc. You can put this at the end of your main program. This procedure should do all the cleaning up that you want to do.

Finally, at the point in your program where the user wants to shut down, execute QUIT. That will fire the ShutdownProc. This will also ensure that the shutdown proc gets executed if the user closes the main application window, or closes Windows, etc.

For more information about using ON SHUTDOWN in this way, see my article at:

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
ahaws

The following code may be useful to put in your .CloseApp or other named procedure that closes the application.

It ensures all open forms are closed in an orderly fashion.
Code:
[COLOR=blue]FOR i = _SCREEN.FormCount TO 1 STEP -1
[tab]RELEASE WINDOWS (_SCREEN.Forms(i).Name)
ENDFOR[/color]
Apologies - I omitted the QUIT command from the example code in the previous post.



FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander[sup]tm[/sup].com
PDFcommander[sup]tm[/sup].co.uk


 
Hi Chris-
>>Apologies - I omitted the QUIT command from the example code in the previous post.

DO I put the QUIT command in my Quitapp procedure? I was having a problem at one point where when I quit my program, it sort of 'hung' - in the command window, 'QUIT' did not work. The sysmenu kind of 'flickers'...eventually I have to 'endTask' to quit the program.
Any suggestions?
thanks
angie
 
angie

Thanks for the star.

The following is just a suggestion as I'm not sure as to what exactly DO clr_sets and DO closedbfs do, so you may need to rearrange the order.

However I would keep the last three commands in the same order.
Code:
[COLOR=blue]On SHUTDOWN [/color][COLOR=green]&& Avoid recursive call[/color][COLOR=blue]

FOR i = _SCREEN.FormCount TO 1 STEP -1
[tab]RELEASE WINDOWS (_SCREEN.Forms(i).Name)
ENDFOR

DO clr_sets [/color][COLOR=green]&& Maybe redundant?[/color][COLOR=blue]
CLOSE ALL [/color][COLOR=green]&& Closes databases[/color][COLOR=blue]
Do closedbfs

CLEAR PROGRAM
CLEAR All
QUIT[/color]



FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander[sup]tm[/sup].com
PDFcommander[sup]tm[/sup].co.uk


 
clr_sets:

Hi Chris-
here is what those programs do:

***********************************************************************
* CLR_SETS clears FoxPro SET statements back to their default
* values
*
***********************************************************************
PROCEDURE clr_sets

ON KEY LABEL F1
RESTORE MACROS
SET BELL ON
SET STATUS BAR ON
SET COLOR TO
SET DELETED OFF
SET ESCAPE ON
SET EXACT ON
SET NEAR OFF
SET NOTIFY ON
SET REPROCESS TO 0
SET SAFETY ON
set talk off
RETURN


closedbfs:

Close Databases
****tables*******************
SET PATH TO m:\occIf Used(who + "desc.dbf")
Use
Endif
If File(who + "desc.dbf")
Delete File (who + "desc.dbf")
Endif
If Used(who + "mast.dbf")
Use
Endif
If File(who + "mast.dbf")
Delete File (who + "mast.dbf")
Endif
If Used(who + "owner.dbf")
Use
Endif
If File(who + "owner.dbf")
Delete File (who + "owner.dbf")
Endif
----also deletes .cdx files


Should I keep in the same order?
Thanks
Angie
 
Angie

To answer your question, that should be fine.

As a general comment, when developing and testing an app, I minimise VFP to the task bar and launch the app from a desktop icon to basically simulate what is likely to happen assuming the application to have been distributed.

Once the testing of the app is complete and the app has been terminated, VFP is restored, further development takes place until the testing cycle starts all over again.

Under those circumstances the procedure CLR_SETS becomes redundant.

FI, you can have SET SAFE OFF in your app, but SET SAFE ON in the development environment and neither has any effect on the other.

As far as the procedure CLOSEDBFS is concerned, CLOSE ALL will close all open databases and tables.

You could then delete any files with DELE FILE path\file*.dbf and DELE FILE path\file*.cdx or depending whats in the folder, DELE FILE path\*.*, remembering to enclose the path\filename in "" should there be any spaces in the path\filename.

So you could replace the procedure with one or two lines of code.

FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander[sup]tm[/sup].com
PDFcommander[sup]tm[/sup].co.uk


 
Hi Angie,

The order of code..

Do closedbfs
QUIT

will do, though no harm in keeping the codes.
The reason is that when you QUIT VFP, the rest of clear sets etc are meaningless, since the user is returned back to the OS and not back to your application.

If QUIT is not used, then, you need to carefully track what needs to be released and what needs to be SET back, before the control is transferred to VFP development environment (probably something related hung your computer in earlier code without QUIT). QUIT is a safe game.

:)

____________________________________________
ramani - (Subramanian.G) :)
 
Thanks Guys!
I appreciate all your help and everything is working as it should! [thumbsup2] Have a great weekend.
Angie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top