Why want my VB6 to shut down when the user has been inactive for a length of time, say 30 min. How can I do it?
Printable View
Why want my VB6 to shut down when the user has been inactive for a length of time, say 30 min. How can I do it?
All 'activity' to an app comes to it via messages from the OS.
If no messages are received then there is no activity.
Trap the messages going to the app by subclassing.
This example uses a Timer control which has limited duration.
Check up on SetTimer and KillTimer api's for longer times.
try something like this in a .BAS module
TO start the process in your Form load event add as the last line in the sub:Code:Public Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_PASTE = &H302
Public WinProcOld As Long
Public Function WinProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' we got here because somebody created "activity"
ResetTimer
WinProc = CallWindowProc(WinProcOld&, hwnd&, wMsg&, wParam&, lParam&)
End Function
Sub SubClassWnd(hwnd As Long)
WinProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WinProc)
End Sub
Sub UnSubclassWnd(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, WinProcOld&
End Sub
Sub ResetTimer ' restart the timer
Form1!Timer1.Enabled = False
Form1!Timer1.Timer = 30000
Form1!Timer1.Timer.Enabled = True
End Sub
In the timer event:Code:Call SubClassWnd(hwnd)
In form unload:Code:' we got here because the timer expired
Unload Me
End
Code:' do this or risk crashing the PC!
Call UnSubCLassWnd(hwnd)
subclassing in VB only works with its own process. you cannot subclass otehr windows outside your app.