System Ideal Time

Posted on 2001-06-21
Last Modified: 2008-02-01
I want to know the System Ideal Time from VB.  

We can find one option in MSN Messanger, that is "Show me AWAY when I am inactive for 5 minutes".

I am trying to simulate this option from my VB program.  I dont want to use the Timer function to check repeatedly.  Becuase this may be resource intensive.

Is there any API (compatible on 9x & NT) to achieve this ?
ie., is there any API which gives the system Ideal time ?

Thanks in advance.

Cheers !!!
Question by:v_natraj_v
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
  • 2
  • 2
  • 2
  • +4

Expert Comment

ID: 6214655

well i dont know if you can use this but this tells you how long windows has been running to the nearest millisecond

Declare Function GetTickCount& Lib "kernel32" ()

i will still think about this

Expert Comment

ID: 6214783

I don't understand what you mean by system ideal time, but if you want to do something similar to that MSN messenger thing, the best way to go is to use a timer. Just set it to 5000 ms and then reset everytime the user does something. The timer is not very resource intensive to run since it runs in its own thread. The only issue might be that you have to reset it a lot.


Expert Comment

ID: 6214784
check out the API setTimer. Here is code I used:

'In module:
Option Explicit

Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Public Sub TimerProc()
  MsgBox "Hi!"
'Replace this with your code you want
'executed every x Seconds
End Sub

'In Form1 (Add a button to the form and leave the default name)
Option Explicit

Private Sub Command1_Click()
  'Timer interval = 5 seconds
  '5000ms = 5s
  SetTimer Me.hWnd, 0, 5000, AddressOf TimerProc
End Sub

hope this helps!
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


Expert Comment

ID: 6214811
and to stop the timer:
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

then in the TimerProc Add this:

KillTimer Form1.hWnd, 0
LVL 26

Expert Comment

ID: 6214836
Are you talking about like screen saver?

If nobody touch computer, like no keystroke or no mouse movement, then it will print message after some time expired?
LVL 16

Expert Comment

ID: 6215620

Expert Comment

ID: 6216478
You just want to know if your program has been inactive for a certain amount of time, yes? Not whether the user has been active at all?

If so, I use the following code to produce an auto-logoff effect after two hours (which is to say, 7200000 milliseconds):

Declarations in frmEditor:
Private mhActivityEvent As Long
Private mhSignalLoopThread As Long

In Form_Load():
mhActivityEvent = CreateEvent(0, 0, 0, 0)

In Form_Unload():
If mhSignalLoopThread <> 0 Then CloseHandle mhSignalLoopThread
CloseHandle mhActivityEvent

In my logon function:
Dim lThreadID As Long
If mhSignalLoopThread = 0 Then mhSignalLoopThread = CreateThread(0, 0, AddressOf mdlTimers.SignalLoop, mhActivityEvent, 0, lThreadID)

In module mdlTimers:
Public Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (ByVal lpEventAttributes As Long, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As Long) As Long
Public Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Any, ByRef lpParameter As Any, ByVal dwCreationFlags As Long, ByRef lpThreadId As Long) As Long
Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long
Private Const WAIT_ABANDONED_0& = &H80&
Private Const WAIT_OBJECT_0& = 0
Private Const WAIT_TIMEOUT& = &H102&
Private Const QS_HOTKEY& = &H80
Private Const QS_KEY& = &H1
Private Const QS_MOUSEBUTTON& = &H4
Private Const QS_MOUSEMOVE& = &H2
Private Const QS_PAINT& = &H20
Private Const QS_POSTMESSAGE& = &H8
Private Const QS_SENDMESSAGE& = &H40
Private Const QS_TIMER& = &H10
Private Const QS_MOUSE& = (QS_MOUSEMOVE _
                            Or QS_MOUSEBUTTON)
Private Const QS_INPUT& = (QS_MOUSE _
                            Or QS_KEY)
Private Const QS_ALLEVENTS& = (QS_INPUT _
                            Or QS_POSTMESSAGE _
                            Or QS_TIMER _
                            Or QS_PAINT _
                            Or QS_HOTKEY)
                            Or QS_PAINT _
                            Or QS_TIMER _
                            Or QS_POSTMESSAGE _
                            Or QS_MOUSEBUTTON _
                            Or QS_MOUSEMOVE _
                            Or QS_HOTKEY _
                            Or QS_KEY)
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long

Public Const WM_USER = &H400

Public Sub SignalLoop(ByRef hEvent As Long)
Dim lRet As Long

    lRet = MsgWaitForMultipleObjects(1, hEvent, 0, 7200000, QS_ALLEVENTS Or QS_ALLINPUT)
    Select Case lRet
    Case WAIT_OBJECT_0
    Case WAIT_OBJECT_0 + 1
        SendMessage frmEditor.hwnd, WM_AUTOLOGOFF, 0, 0
        Exit Do
    End Select

End Sub

And subclass the form to let me grab and deal with the WM_AUTOLOGOFF message.

I hope I have included all the necessary declarations. Bear in mind that this is manual multi-threading in VB - VB hates that, and will crash in debug mode, so disable this code when debugging.

With a little work, you may be able to adapt that to your purposes. If you do need to check all user activity on the system, well, I guess that will need a somewhat different technique.

(Do I remember what I'm using that event I created for? No, actually, I don't, but maybe I just need something to wait on. Now that I've noticed that, I'll be checking the code again.)

Author Comment

ID: 6217027
Hi All,


EDDYKT - Exactly.  I am expecting the same kind of functionality.

TravisHall -- I want to trap the User Inactive time not on my program.  

Mine is a small program reside on the Systray.  There will be no user interface for this program.

It will be better if we could able to trap how the windows invokes Screen Saver, by identifying user idel time.

Cheers !!!

Accepted Solution

TravisHall earned 100 total points
ID: 6217060
Well, there's a couple of ways to do this, depending on your operating system. Do you happen to be running Windows 2000, 98 or Me? Under these, I believe, you can determine whether the screen saver is running using the SystemParametersInfo API function, with the SPI_GETSCREENSAVERRUNNING action parameter.

Under Windows NT 4.0 (and maybe earlier), it's harder. You can use the OpenDesktop API function (with a desktop name of "screen-saver"), and if it returns a valid handle, the screen-saver is running. If it doesn't return a valid handle, check the error returned (with GetLastError), and if it is ERROR_ACCESS_DENIED, the screen-saver is running, but if it is anything else, it isn't. Don't forget to close your desktop handle, if you get one, with CloseDesktop. Note, however, that there is a bug with NT SP4 that may stop this from working.

Or, you can go the whole hog and write your own screen saver. Your screen saver could, in turn, invoke a standard screen saver, after it does whatever you want it to do.

Author Comment

ID: 6226323
I have removed the option "Show me away when I am inactive for 'n' minutes" time being from my program.

Instead I applied TravisHall idea to trap the Screen Saver in background and made my application inactive during that time.

TravisHall - Thanks.  If you find the solution for what I am exactly look for then post it.

Cheers !!!

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 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…
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…

738 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