Click here to Skip to main content
6,593,923 members and growing! (13,498 online)
Email Password   helpLost your password?
General Reading » Hardware & System » System     Beginner License: The Code Project Open License (CPOL)

Calculate system idle time without hooks

By Qaiser_Iftikhar

An article to demonstrate how to calculate system idle time without installing any system wide keybaord/mouse hooks.
VB 7.x, VB 8.0, VB 9.0, VB 10.NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, Dev
Version:3 (See All)
Posted:27 Jun 2009
Views:2,817
Bookmarked:10 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 1.51 Rating: 5.00 out of 5

1

2

3

4
2 votes, 100.0%
5

Introduction

This article described code for calculating for how long a system has been idle. This is useful to do background work etc. There might be other similar articles on the web and/or CodepPoject; if you find a better way, please do let me know. The attached sample is a simple WinForms application to demonstrate the code.

Background

I was browsing the web and encountered a couple of articles on how to calculate idle time, but they were doing it using system wide hooks. We don't want to install hooks, do we? Especially for a simple task like calculating system idle time. So, the result is this article.

Using the code

The code is very simple, just a couple of Windows API function calls.

Dim idleStruct As LASTINPUTINFO
idleStruct.cbSize = Marshal.SizeOf(idleStruct)
'check if we are able to calculate time.
If GetLastInputInfo(idleStruct) Then
    Dim sysIdleTime As Integer = GetTickCount() - idleStruct.dwTime
    Dim totalTime As New TimeSpan(sysIdleTime * 10000)
End If

All we need to do is create an object of the LASTINPUTINFO structure. Before passing it as a parameter, set its size using the Marshal.SizeOf method. The function GetLastInputInfo is used to populate the dwTime property of the LASTINPUTINFO structure. If the function is successful, it returns true; otherwise false. Once the dwTime is populated, we call another function GetTickCount; what this does is it calculates the time since the Windows session was started. And, the API declarations are as follows:

''' <summary>
''' Structure for last input infomation.
''' </summary>
''' <remarks>More info about
''' it http://msdn.microsoft.com/en-us/library/ms646272(VS.85).aspx
''' </remarks>
<StructLayout(LayoutKind.Sequential)> _
       Public Structure LASTINPUTINFO
    Public cbSize As Integer
    Public dwTime As Integer
End Structure


''' <summary>
''' Function to get the last input value.
''' </summary>
''' <param name="lii"></param>
''' <returns>Returns true on sucess and false on failure.</returns>
''' <remarks>More info about it http://msdn.microsoft.com/
'''      en-us/library/ms646302(VS.85).aspx</remarks>
Public Declare Function GetLastInputInfo Lib "User32.dll" _
              (ByRef lii As LASTINPUTINFO) As Boolean


''' <summary>
''' Get the time since windows started.
''' </summary>
''' <returns></returns>
''' <remarks>http://msdn.microsoft.com/en-us/library/ms724408(VS.85).aspx</remarks>
Public Declare Function GetTickCount Lib "kernel32" _
               Alias "GetTickCount" () As Integer

Conclusion

I hope this article helps my fellow developers. If you like this article, please do vote for it. And, if you find a bug or improvement, do let me know.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Qaiser_Iftikhar


Member

Occupation: Software Developer
Company: GWDMedia
Location: United Kingdom United Kingdom

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Jun 2009
Editor: Smitha Vijayan
Copyright 2009 by Qaiser_Iftikhar
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project