System Ideal Time

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 !!!
Who is Participating?
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.

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

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.

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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!
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
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?
Richie_SimonettiIT OperationsCommented:
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.)
v_natraj_vAuthor Commented:
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 !!!
v_natraj_vAuthor Commented:
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 !!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.