Solved

Idle event in VB

Posted on 2001-09-14
13
363 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 51

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 51

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 51

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

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
 
LVL 51

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 70

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

830 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