Solved

Idle event in VB

Posted on 2001-09-14
13
351 Views
Last Modified: 2012-08-13
Hi experts,

Is there any way to perform an idle time checking in VB base on the value from INI files ? Each time the idle time exceeds, the login form will fire to the user.


Regards,
Bluers


0
Comment
Question by:bluers
  • 4
  • 2
  • 2
  • +4
13 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6482138
Hi bluers,

You can try this to check the time difference:

Dim StartTime As Date
Dim StopTime As Date

StartTime = Now()

StopTime = StartTime + DateValue("00:05:00") 'hh:mm:ss , here add 5 minutes

In a Sub or Timer

Call CheckValue

Private Sub CheckValue
     DoEvents
     If Now > StopTime Then
          'Prompt Login Window

     End If
End Sub

'Hope will help.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6482148
Here is the code to read/write from an INI file:

http://www.vbcode.com/asp/showsn.asp?theID=1404

'Hope will help.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6482153
Hi bluers,

Sorry for the first post as it refer to compare Time values. By the way, the concept are similar:

Private Sub CheckValue(byVal myValue)
    DoEvents
    If myValueFromINI <> myValue Then
         'Prompt Login Window

    End If
End Sub

regards.
0
 

Author Comment

by:bluers
ID: 6482163
Hi ryancys,

But where should i put the timer object because my application is a MDI one ?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6482171
When you say idle time, do you mean when the user fails to move the mouse or press a key within a specified timeframe? If so then this is a bit more complicated.
0
 

Author Comment

by:bluers
ID: 6482181
Hi TimCottee,

Yes, thats what I mean and the timeframe comes from INI files ?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6482184
You can try the GetTickCount API in order to catch a time in milisecond between a certain processes.
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6482195
You're trying to write a 'quiesce' function right? Ie if the user hasn't touched their app (mouse/keyboard) and it hasn't performed any processing for a certain amount of time, it requires a logon to use again?

The best way to do this is to use the GetLastInputInfo API call. This returns the time of the last input event. Unfortunately it's win2K only. Boo.

To do this for all platforms you need to hook the mouse and keyboard queues and handle messages as they come in.

I'll try and rustle up a code sample for you if you like
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6482200
Ok, now I have done this before and it requires a bit of thinking about. You need to hook the windows keyboard and mouse messages and determine whether these are taking place inside the appropriate timeframe. The best way I have found of doing this is the vbalhook dll from http://vbaccelerator.com/codelib/hook/vbalhook.htm this will allow you to add a timer or if you want (and I did) use the ssubtmr timer also from vbaccelerator. You can then monitor the keyboard and mouse in the callback function(s) and reset the timer as necessary. When the timer event fires it is because no message has been processed within its interval and your form can be displayed.
0
 
LVL 6

Accepted Solution

by:
andyclap earned 20 total points
ID: 6482269
I Couldn't resist writing this (It'll be useful for my toolkit anyway).

To demonstrate it, Create a new project
Create a new module, call it something like modQuiesceHook

Add the following code.

----------------------------------
Option Explicit

Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

Private Const WH_MOUSE = 7
Private Const WH_KEYBOARD = 2
Private Const HC_ACTION = 0

Private mlMouseHook As Long
Private mlKeyboardHook As Long

Private mdtLastInputTime As Date

Public Sub InitQuiesceTimer()

    mlMouseHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseHook, 0, App.ThreadID)
    mlKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardHook, 0, App.ThreadID)
   
End Sub
Public Sub CloseQuiesceTimer()

    UnhookWindowsHookEx mlMouseHook
    UnhookWindowsHookEx mlKeyboardHook

End Sub
Public Function LastInputTime() As Date

    LastInputTime = mdtLastInputTime
   
End Function
Private Function MouseHook(ByVal lCode As Long, ByVal lWP As Long, ByVal lLP As Long) As Long

    If lCode = HC_ACTION Then
        mdtLastInputTime = Now
    End If
    CallNextHookEx mlMouseHook, lCode, lWP, lLP
   
End Function
Private Function KeyboardHook(ByVal lCode As Long, ByVal lWP As Long, ByVal lLP As Long) As Long

    If lCode = HC_ACTION Then
        mdtLastInputTime = Now
    End If
    CallNextHookEx mlKeyboardHook, lCode, lWP, lLP
   
End Function


----------------------------------

To test it out, in form1 add a timer (timer1) with an interval of 1000 (ie one second), and a label (label1).

Add the following code to the form:
-------------------------------------
Option Explicit

Private Sub Form_Load()

    InitQuiesceTimer
   
End Sub

Private Sub Form_Unload(Cancel As Integer)

    CloseQuiesceTimer
   
End Sub

Private Sub Timer1_Timer()

    Label1.Caption = Format$(LastInputTime, "hh:nn:ss")
   
End Sub

-------------------------------------

Now when you run this app, it will show you the time the last input message occurred.
You can do things like datediff with this time to work out if you should quiesce, but I'll leave that, plus ini file handling (although I'd recommend using the registry), to you.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6482353
See this download in which I logoff an application after some time. You can reuse it to re-display the logon form instead of logging out.

http://www.universalthread.com/wconnect/wc.dll?FournierTransformation~2,54,Forum=21,28,1474
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7208416
Hi bluers,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept andyclap's comment(s) as an answer.

bluers, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7241232
Per recommendation, force-accepted.

Netminder
CS Moderator
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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now