Solved

Better Algorithm?

Posted on 2002-05-19
5
176 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:eeyore7250
5 Comments
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 7020992
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.
0
 

Expert Comment

by:GuruVB
ID: 7021049
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
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 7021083
I use the vbalhook.dll from http://www.vbaccelerator.com/codelib/hook/vbalhook.htm as this provides a more event driven method than the constant polling method you use. Essentially once you have installed the hooks to the keyboard and the mouse you can then set a timer (ccrp has a great timer control that doesn't need to be placed in a form: http://www.mvps.org/ccrp)
0
 
LVL 4

Author Comment

by:eeyore7250
ID: 7025972
Ok gents, great ideas. But, which method hogs the least resources?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7026662
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now