Link to home
Start Free TrialLog in
Avatar of eeyore7250
eeyore7250

asked on

Better Algorithm?

I have an application that needs to lock when nothing has been done for a certain time. Here is a basic outline of what I am doing, and I would like to know if this is the best way to do it?:

PS. I run this in Microsoft Access and it hogs a reasonable amount of resources


Option Explicit

Private intLock As Integer

Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)


Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Type Msg
    hwnd As Long
    Message As Long
    wParam As Long
    lParam As Long
    time As Long
    PT As POINTAPI
End Type


Private Const WM_MOUSEMOVE = &H200
Private Const WM_KEYUP = &H101
Private Const WM_KEYDOWN = &H100
Private Const WM_QUIT = &H12
Private Const dblMINUTE As Double = 0.000694444

Private Function fnMessageLoop() As Boolean
'
' Message Loop function.
'     Returns: Boolean, always True.
'     Inputs: None.
'    
' Author: Nick Howard
' Version: 1.0
'
Dim dblTime As Double
Dim lngMsgsCnt As Long
Dim lngMsg As Long
Dim aMsg As Msg
    lngMsgsCnt = 0
    dblTime = CDbl(Now)
    Do
        lngMsg = PeekMessage(aMsg, 0, 0, 0, 0)
        lngMsgsCnt = lngMsgsCnt + 1
        If aMsg.Message <> WM_MOUSEMOVE Then
            If aMsg.hwnd = hwnd Then
                intLock = 0
                dblTime = CDbl(Now)
                Debug.Print aMsg.Message & Now
            Else
                If dblTime + dblMINUTE < CDbl(Now) Then
                    dblTime = CDbl(Now)
                    intLock = intLock + 1
                End If
            End If
        End If
        DoEvents
        If intLock = 1 Then
            MsgBox "time up"
        End If
    Loop Until aMsg.Message = WM_QUIT
    fnMessageLoop = True
End Function
Avatar of Valliappan AN
Valliappan AN
Flag of India image

Just a suggestion:

Can you do the locking part, by creating a Screen Saver program, and implementing the locking code into the Screen Saver program?

Cheers.
Avatar of GuruVB
GuruVB

Hi,

Your code is not bad.

I would say Use SysInfo.OCX  in u r VB program and that ocx has lot's of events.

To lock u can put u r code in SysInfo1_PowerSuspend() event or what ever is appropriate.

Thanks and kind regards,
GuruVB
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eeyore7250

ASKER

Ok gents, great ideas. But, which method hogs the least resources?
An event driven method such as the one I use is far less resource intensive than a polling method such as the one you have used.