Solved

Better Algorithm?

Posted on 2002-05-19
5
179 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA to copy paste columns form one file to other 20 91
How to Add / Edit Windows Menu 4 67
clicking a shape in a frame array vb6 3 46
Help me. 3 60
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 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