I want to write into my application a way to shut itself down (not the PC) if it hasn't been used for a set period of time, does anyone know how this can be done?
Printable View
I want to write into my application a way to shut itself down (not the PC) if it hasn't been used for a set period of time, does anyone know how this can be done?
Well here's for the shut down part, but for checking any activity, i guess mouse movement is enough? You could use Getcursorpos and check if it has changed...Code:'In declarations
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Enum exitmode
Shutdown = 0
Restart = 1
Suspend = 2
End Enum
'In code
Sub ExitWindows(how As exitmode)
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Select Case how
Case 0
t& = ExitWindowsEx(EWX_SHUTDOWN, 0)
Case 1
t& = ExitWindowsEx(EWX_REBOOT Or EXW_FORCE, 0)
Case 2
SetSystemPowerState 1, 1
End Select
End Sub
Thanks but I've got that one and it shuts down the PC, which is not what I want. I just want to close my application.
Here is a simple solution and the program will automatically terminal after 1hour:
Code:Option Explicit
Private tCnt As Long
Private xForm As Form
Private Sub Form_Load()
Timer1.Interval = 60000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If tCnt > 60 Then
For Each xForm In Forms
Unload Me
Next
End
Else
tCnt = tCnt + 1
End If
End Sub
Right, but won't this shut down even if someone is still using it. Is there not a way to check the last time the application was used????
Hi, why don't you merge the two ideas together...
Make tCnt a Global scope then you can check for mouse positions changes, keyboard input or even form loads to reset the count in tCnt. This will make the application shut down after 1 hour of stand still... You can also change the delay but you surely already knew this one :-)
Juz look like this
Code:'Module file
Option Explicit
Global tCnt as Long
Public RESET_COUNTER ()
tCnt = 0
End Sub
'Form
Option Explicit
Private xForm As Form
Private Sub Form_Load()
Timer1.Interval = 60000
Timer1.Enabled = True
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
RESET_COUNTER
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
RESET_COUNTER
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
RESET_COUNTER
End Sub
Private Sub Timer1_Timer()
If tCnt > 60 Then
For Each xForm In Forms
Unload Me
Next
End
Else
tCnt = tCnt + 1
End If
End Sub