Solved

Better Algorithm?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

862 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

23 Experts available now in Live!

Get 1:1 Help Now