Go Premium for a chance to win a PS4. Enter to Win


System Ideal Time

Posted on 2001-06-21
Medium Priority
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
  • 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!
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

782 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