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

Need To Close Application after x Minutes of Noactivity

Status
Not open for further replies.

KirkKelly

Programmer
Jan 5, 2001
15
US
I have an exe that needs to have the ability to close itself down after there is no user activity for a set period of time. Anybody have any idea's how to start or samples?

Kirk
 
A lot depends on the complexity of your application and which framework (if any) you are using. It also depends on what you define as "user activity" - e.g. does just bringing the form to top (or maximizing) count?

Rick
 
I have an app that does this by using keypress on the form, and a timer. Here's a simplified example:

The form's KeyPreview needs to be set to .T.

In Keypress event:
Thisform.KeyPressed = 1
DODEFAULT()

In timer's Timer event:
IF ThisForm.KeyPressed = 0
*... shut down code here
ThisForm.Release
ELSE
ThisForm.KeyPressed = 0
ENDIF

Then set the timer's interval to however milliseconds you want it to check. (60,000 ms ~ 1 minute)

Dave S.
 
I have one of these active in my own app. Here's what I did:

I created a shutdown procedure and called it CloseApp.prg. At the beginning of my program, I issue the command ON SHUTDOWN DO CloseApp.prg. This procedure will fire whenever the user tries to close VFP by clicking the X in the corner or selecting QUIT from the File menu.

You will need to subclass the TIMER control with a procedure in the TIMER event. This event will call your CloseApp.prg with an optional parameter (in my case I simply pass it .T.) that will notify CloseApp that the app is being forced to shut down and should therefore abort all saving options and just shut down.

You will also need to add the following properties to your timer class:

idle_since: contains a DATETIME value that indicates when the last actual activity took place.
last_x: contains the last known X coordinate of the mouse
last_y: contains the last known Y coordinate of the mouse
lastkey: contains the last key pressed on the keyboard
max_idle: number of seconds the app will remain idle before shutting down

In the TIMER event of the class, place this code:
Code:
LOCAL new_x,new_y,cur_key
new_x=mcol() && Current mouse column
new_y=mrow() && Current mouse row
cur_key=lastkey() && Last key pressed on keyboard
if new_x=this.last_x and new_y=this.last_y and cur_key=this.lastkey
	*nothing has changed, we are idle
	if datetime()-this.idle_since>this.max_idle
		this.enabled=.F. && Don't run this code anymore
		do closeapp with .T. && Force app shutdown
		return
	endif
else
	* Something has changed, update the idle_since property
	this.idle_since=datetime()
	this.last_x=new_x
	this.last_y=new_y
	this.lastkey=cur_key
endif

Before entering your event loop, you will then use CreateObject() to make an instance of your Timer event. The other thing you'll need to decide is how often to have the event fire. I have the INTERVAL property set to 100 myself, and it doesn't seem to slow the app down at all.

Hope that helps!
 
I apologize for the poor width control in the above code. 3 of the lines wrap around to the next line, and if you do a direct cut/paste it will generate errors. Be sure to keep those on one line. :eek:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top