Solved

Better Algorithm?

Posted on 2002-05-19
5
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

729 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