How to get system idle time or get notified after the system being idle for an extended time 
Author Message
 How to get system idle time or get notified after the system being idle for an extended time

I am using VB6 to write a COM dll or ocx(might be used in a outlook custom
form), this component  will connect to an enterprise database server, so I
would like the component to disconnect from the data server and exit after a
certain idle time. So either I must be able to get the system idle time by
calling to some windows service  function? or somehow get an event or
notification after the system is idle for some time, the least desirable
solution is to trap key and mouse event in all controls and forms.Any
suggestion?


Thu, 15 Nov 2001 03:00:00 GMT  
 How to get system idle time or get notified after the system being idle for an extended time
Yes try using Connection Pooling that will basically remove your problem.

Using the ODBC driver for you database you will be able to enable connection
pooling and setting a time for how long it should be held open after which
the ODBC-manager will close it by it self.


Quote:
> I am using VB6 to write a COM dll or ocx(might be used in a outlook custom
> form), this component  will connect to an enterprise database server, so I
> would like the component to disconnect from the data server and exit after
a
> certain idle time. So either I must be able to get the system idle time by
> calling to some windows service  function? or somehow get an event or
> notification after the system is idle for some time, the least desirable
> solution is to trap key and mouse event in all controls and forms.Any
> suggestion?



Fri, 16 Nov 2001 03:00:00 GMT  
 How to get system idle time or get notified after the system being idle for an extended time
thank you anyway, but I've already solved the problem. Here is the code

'''''''''''''''''''
Option Explicit

Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal
nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal
nIDEvent As Long) As Long

Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA"
(ByVal idHook As Long, _
  ByVal lpfn As Long, ByVal hMod As Long, _
  ByVal dwThreadId As Long) As Long

Declare Function CallNextHookEx Lib "user32" (ByVal hhk As Long, _
  ByVal nCode As Long, _
  ByVal wParam As Long, _
  ByVal lParam As Long) As Long

Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As Long) As
Boolean
' BOOL= BOOLEAN ?

'''''''''' monitor for ellapse idle time
Private Const idleclick As Integer = 1000   ' in ms

Private mtickperiod As Integer

Private idlecountermax As Integer
Private idlecounter As Integer

Private TimerId As Long

Private mhookid As Long  ' for mouse
Private khookid As Long  ' for keyboard

Public ToNotifys As Collection  'for use only in idlenotify class

Private Function MouseProc(ByVal nCode As Long, _
  ByVal wParam As Long, ByVal lParam As Long) As Long

    If nCode < 0 Then
        MouseProc = CallNextHookEx(mhookid, nCode, wParam, lParam)
    Else

        idlecounter = 0
        MouseProc = CallNextHookEx(mhookid, nCode, wParam, lParam)
    End If

End Function

Private Function KeyProc(ByVal nCode As Long, _
  ByVal wParam As Long, ByVal lParam As Long) As Long

    If nCode < 0 Then
        KeyProc = CallNextHookEx(khookid, nCode, wParam, lParam)
    Else
        idlecounter = 0
        KeyProc = CallNextHookEx(khookid, nCode, wParam, lParam)
    End If

End Function

Private Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent
As Long, ByVal lngSysTime As Long)
Dim tmp As IdleNotifer

    If idEvent = TimerId And idlecounter < idlecountermax Then
        idlecounter = idlecounter + 1

        If idlecounter = idlecountermax Then
            'frm.WindowState = vbMinimized
            For Each tmp In ToNotifys
                tmp.RaiseIdle
            Next
        Else
            For Each tmp In ToNotifys
                tmp.RaiseTick idlecounter, idlecountermax - idlecounter
            Next
        End If
    End If
End Sub

Public Sub RestartIdleMonitor()
    Debug.Assert TimerId <> 0

    idlecounter = 0

End Sub

Public Sub EndIdleMonitor() 'must call this before program end
    If TimerId <> 0 Then
        KillTimer 0, TimerId
        TimerId = 0
    End If

    Set ToNotifys = Nothing

    If mhookid <> 0 Then UnhookWindowsHookEx mhookid
    mhookid = 0
    If khookid <> 0 Then UnhookWindowsHookEx khookid
    khookid = 0

End Sub

Public Function StartIdleMonitor(ltickperiod As Integer, ltickcount As
Integer) As Boolean
    'WH_MOUSE            7
    If mhookid = 0 Then _
        mhookid = SetWindowsHookEx(7, AddressOf MouseProc, 0, App.ThreadID)
        'WH_KEYBOARD         2
    If khookid = 0 Then _
        khookid = SetWindowsHookEx(2, AddressOf KeyProc, 0, App.ThreadID)

    If TimerId <> 0 Then
        KillTimer 0, TimerId
        TimerId = 0
    End If

    idlecountermax = ltickcount
    If idlecountermax < 2 Then idlecountermax = 2

    idlecounter = 0

    If ltickperiod < idleclick Then
        mtickperiod = idleclick
    Else
        mtickperiod = ltickperiod
    End If

    If mhookid <> 0 And khookid <> 0 Then
        TimerId = SetTimer(0, 0, mtickperiod, AddressOf TimerProc)
        If TimerId <> 0 Then
            StartIdleMonitor = True
            Exit Function
        End If
    End If

    EndIdleMonitor
    StartIdleMonitor = False

End Function

Public Function tickperiod() As Integer
    tickperiod = mtickperiod
End Function

Public Function TickCount() As Integer
    TickCount = idlecountermax
End Function

''''''''''''''''''''''''''''''''''''''''
Option Explicit

Public Event IdleNotify()

' If you do not need this, comment out it
Public Event TickNotify(ByVal curTick As Integer, ByVal remainTick As
Integer)

Private index As Integer

Friend Sub RaiseTick(ByVal curTick As Integer, ByVal remainTick As Integer)
    RaiseEvent TickNotify(curTick, remainTick)
End Sub

Friend Sub RaiseIdle()
    RaiseEvent IdleNotify
End Sub

' call this before set nothing
Public Sub EndNotify()
    ToNotifys.Remove "H" + Hex(index)
End Sub

Private Sub Class_Initialize()

    If ToNotifys Is Nothing Then
        Set ToNotifys = New Collection
    End If

    index = ToNotifys.Count
    ToNotifys.Add Me, "H" + Hex(index)
End Sub



Sun, 25 Nov 2001 03:00:00 GMT  
 How to get system idle time or get notified after the system being idle for an extended time
Ill check that out but I never been able to hook globally like that without
something crashing.

--
Donald A. Herman
Software - Scheduler Pro, Disk Cataloger
http://members.tripod.com/~Don_Herman/
Home Page, VB5 & 6 Resources, Information and Links
http://www.fcs-net.com/dherman/index.htm

--

Quote:
> thank you anyway, but I've already solved the problem. Here is the code

> '''''''''''''''''''
> Option Explicit

> Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal
> nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As
Long
> Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal
> nIDEvent As Long) As Long

> Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA"
> (ByVal idHook As Long, _
>   ByVal lpfn As Long, ByVal hMod As Long, _
>   ByVal dwThreadId As Long) As Long

> Declare Function CallNextHookEx Lib "user32" (ByVal hhk As Long, _
>   ByVal nCode As Long, _
>   ByVal wParam As Long, _
>   ByVal lParam As Long) As Long

> Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As Long) As
> Boolean
> ' BOOL= BOOLEAN ?

> '''''''''' monitor for ellapse idle time
> Private Const idleclick As Integer = 1000   ' in ms

> Private mtickperiod As Integer

> Private idlecountermax As Integer
> Private idlecounter As Integer

> Private TimerId As Long

> Private mhookid As Long  ' for mouse
> Private khookid As Long  ' for keyboard

> Public ToNotifys As Collection  'for use only in idlenotify class

> Private Function MouseProc(ByVal nCode As Long, _
>   ByVal wParam As Long, ByVal lParam As Long) As Long

>     If nCode < 0 Then
>         MouseProc = CallNextHookEx(mhookid, nCode, wParam, lParam)
>     Else

>         idlecounter = 0
>         MouseProc = CallNextHookEx(mhookid, nCode, wParam, lParam)
>     End If

> End Function

> Private Function KeyProc(ByVal nCode As Long, _
>   ByVal wParam As Long, ByVal lParam As Long) As Long

>     If nCode < 0 Then
>         KeyProc = CallNextHookEx(khookid, nCode, wParam, lParam)
>     Else
>         idlecounter = 0
>         KeyProc = CallNextHookEx(khookid, nCode, wParam, lParam)
>     End If

> End Function

> Private Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal
idEvent
> As Long, ByVal lngSysTime As Long)
> Dim tmp As IdleNotifer

>     If idEvent = TimerId And idlecounter < idlecountermax Then
>         idlecounter = idlecounter + 1

>         If idlecounter = idlecountermax Then
>             'frm.WindowState = vbMinimized
>             For Each tmp In ToNotifys
>                 tmp.RaiseIdle
>             Next
>         Else
>             For Each tmp In ToNotifys
>                 tmp.RaiseTick idlecounter, idlecountermax - idlecounter
>             Next
>         End If
>     End If
> End Sub

> Public Sub RestartIdleMonitor()
>     Debug.Assert TimerId <> 0

>     idlecounter = 0

> End Sub

> Public Sub EndIdleMonitor() 'must call this before program end
>     If TimerId <> 0 Then
>         KillTimer 0, TimerId
>         TimerId = 0
>     End If

>     Set ToNotifys = Nothing

>     If mhookid <> 0 Then UnhookWindowsHookEx mhookid
>     mhookid = 0
>     If khookid <> 0 Then UnhookWindowsHookEx khookid
>     khookid = 0

> End Sub

> Public Function StartIdleMonitor(ltickperiod As Integer, ltickcount As
> Integer) As Boolean
>     'WH_MOUSE            7
>     If mhookid = 0 Then _
>         mhookid = SetWindowsHookEx(7, AddressOf MouseProc, 0,
App.ThreadID)
>         'WH_KEYBOARD         2
>     If khookid = 0 Then _
>         khookid = SetWindowsHookEx(2, AddressOf KeyProc, 0, App.ThreadID)

>     If TimerId <> 0 Then
>         KillTimer 0, TimerId
>         TimerId = 0
>     End If

>     idlecountermax = ltickcount
>     If idlecountermax < 2 Then idlecountermax = 2

>     idlecounter = 0

>     If ltickperiod < idleclick Then
>         mtickperiod = idleclick
>     Else
>         mtickperiod = ltickperiod
>     End If

>     If mhookid <> 0 And khookid <> 0 Then
>         TimerId = SetTimer(0, 0, mtickperiod, AddressOf TimerProc)
>         If TimerId <> 0 Then
>             StartIdleMonitor = True
>             Exit Function
>         End If
>     End If

>     EndIdleMonitor
>     StartIdleMonitor = False

> End Function

> Public Function tickperiod() As Integer
>     tickperiod = mtickperiod
> End Function

> Public Function TickCount() As Integer
>     TickCount = idlecountermax
> End Function

> ''''''''''''''''''''''''''''''''''''''''
> Option Explicit

> Public Event IdleNotify()

> ' If you do not need this, comment out it
> Public Event TickNotify(ByVal curTick As Integer, ByVal remainTick As
> Integer)

> Private index As Integer

> Friend Sub RaiseTick(ByVal curTick As Integer, ByVal remainTick As
Integer)
>     RaiseEvent TickNotify(curTick, remainTick)
> End Sub

> Friend Sub RaiseIdle()
>     RaiseEvent IdleNotify
> End Sub

> ' call this before set nothing
> Public Sub EndNotify()
>     ToNotifys.Remove "H" + Hex(index)
> End Sub

> Private Sub Class_Initialize()

>     If ToNotifys Is Nothing Then
>         Set ToNotifys = New Collection
>     End If

>     index = ToNotifys.Count
>     ToNotifys.Add Me, "H" + Hex(index)
> End Sub



Sun, 25 Nov 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Getting idle time of system/os

2. How to get system idle time or get notified after the system being idle for an extended time

3. System Idle time

4. System Idle Time modification...

5. system idle time

6. System Idle Time modification...

7. How to detect system idle time?

8. How to determine system idle time without hooks?

9. Monitoring System Idle Time

10. execute on system idle time

11. Get system idle time

12. Exception in System.Windows.Forms while application is idle

 

 
Powered by phpBB® Forum Software