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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you run a procedure before last user exits system 5

Status
Not open for further replies.

booleanist

Programmer
Aug 15, 2005
3
US
Hello,

I'm using the database container in VFP 7.0 (SP1) project. Is there an indicator or any way to test to see if a user is the last person leaving the system? The only way I can think of is to close the database and have the exit routine attempt to reopen the database container exclusively and see if an error occurs.

1) Is there a better way?
2) Can the database container execute code on shutdown?

Thanks for your time,

Dave

The 2nd mouse gets the cheese.
 
One option would be to maintain a table where a record gets updated with a flag something like 'logged_in' every time someone runs the app. As they exit, the flag could be set to .F. or the record deleted.
You could then count how many .T.'s there are and run the routine if there is only one.

Another way may be to see how many open file handles there are for your app/exe using the NetFileEnum() funtion.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 

booleanist,

Your idea of closing the DBC and trying to re-open it exclusively is not so hot. Apart from performance considerations, you will risk locking out users occasionally.

There's no way for a database to execute code on shutdown, but your application can do that, either from the ON SHUTDOWN command, or simply by putting code at the point in the app where the user signals they want to quit.

I think Dave Summers idea is better: maintain a table that records how many people are logged in. Just be aware that if one user crashes out of the app, their record will not be cleared from the login table (that doesn't apply to me because my applications never crash <I wish>).

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Hi all,

That table of logged in users is a way. Don't have a field set .T., simply lock the user's record. Locks are unlocked even when an app crashes, so the number of records locked is a very good sign of the number of users logged in. In the end you must encapsule an Open Database ... Exclusive inside a Try..Catch block within a loop exited when succeeding.

There are database events since VFP7, you can turn these on. But then again, the CloseData()-Event get's triggerd by any Close Database, not only the one of the last user.

There you have an instrument to not only detect database usage by your app, but also from other apps and by users working with a VFP IDE. But ODBC access to VFP data is not possible with database events turned on.

Nevertheless I'd not really recommend maintainance routines triggered by the last user log off.

Let's say you start a backup at that event. If your database get's corrupted (index corruption, disk crash, whatever) and all users exit the app because of that, you may not want to make a backup of that database state. Well, at least not replacing the last backup by that.

I'd recommend to not do maintainance tasks within the application, but from a separately running administration application. That can have additional information, eg the cause of the overall application shutdown etc.

Bye, Olaf.
 
If anyone reports a problem, just say, "Well, you weren't supposed to do that!
 
Hi,

Another way would be to create a low level file using fcreate() to a log dir when a user logs on and then close and delete the file when they log off - by means suggested by Mike where the user signals they want to quit.

Once the file is open only the creator in that same session can close the file. So whilst the file is open it can't be deleted by anyone.

If the program crashes (heaven forbid) the file is then released thus enabling any other user session to delete it. No problems then with a flag still set to .T.

If you want to check to see if any users are still logged adir() the log dir and then try to delete the files one by one. Here you need to detect any errors returned - VFP5 returns "File access is denied" - other user session or "File is in use" - the session you are in if files can't be deleted. Count the No of failed deletions you get remembering it will always return at least 1.

If you get 2> other user sessions are still logged.
1 = You are the last user run your routine.

This method works fine for me in VFP5. I use this method to check for logged users before reindex/backups routines.

You can also use this method to lock all users out whilst in a maintenance mode.

Just another idea that could be used.

Holiam
 

Lots of good info - thanks for your time one and all.

Dave

The 2nd mouse gets the cheese.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top