Solved

System Ideal Time

Posted on 2001-06-21
10
384 Views
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 !!!
Natraj
0
Comment
Question by:v_natraj_v
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 2

Expert Comment

by:Microsoft
ID: 6214655
erm...

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

Expert Comment

by:Z_Beeblebrox
ID: 6214783
Hi,

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.

Zaphod.
0
 
LVL 6

Expert Comment

by:JonFish85
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!
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 6

Expert Comment

by:JonFish85
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
0
 
LVL 26

Expert Comment

by:EDDYKT
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?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6215620
ping...
0
 
LVL 2

Expert Comment

by:TravisHall
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)
Private Const QS_ALLINPUT& = (QS_SENDMESSAGE _
                            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 Const WM_AUTOLOGOFF = WM_USER

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

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

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.)
0
 
LVL 1

Author Comment

by:v_natraj_v
ID: 6217027
Hi All,

Thanks.

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 !!!
Natraj
0
 
LVL 2

Accepted Solution

by:
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.
0
 
LVL 1

Author Comment

by:v_natraj_v
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 !!!
Natraj
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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

786 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