Click here to Skip to main content
Click here to Skip to main content

Calculate system idle time without hooks

, 27 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
An article to demonstrate how to calculate system idle time without installing any system wide keybaord/mouse hooks.

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)

Share

About the Author

Qaiser_Iftikhar
Software Developer GWDMedia
United Kingdom United Kingdom
Started as a C++ developer in 2003 and moved to Java for a little while. Started working in .net/C# in 2005 and still in love with it. Working with wpf for past 3-4 years.

Working at GWDMedia for past 6 years, www.gwdmedia.com, as lead developer for client/desktop development.

Does a bit of Android development in spare time.

Love watching Sport and like watching films with a good storyline.

Comments and Discussions

 
QuestionYour code needs a slight change. I've spotted a problem. PinmemberJohn G. Lynch25-Jan-13 2:33 
Because sysIdleTime is defined as an integer it is limited to a maximum value of:
2,147,483,647

Dim sysIdleTime As Integer = GetTickCount() - idleStruct.dwTime

Dim totalTime As New TimeSpan(sysIdleTime * 10000)

You will get an overflow error once it has ticked up to 3:34 because 214,845 x 10,000 = 2,148,450,000 which is greater than the Interger maximum value of 2,147,483,647.

Great code otherwise.


Thanks,

John
GeneralMy vote of 5 PinmemberABDUL QADER FAROOQI8-Nov-12 0:59 
QuestionIs it possible in Web forms? Pinmemberdeja_anbu12-Feb-11 23:30 
AnswerRe: Is it possible in Web forms? PinmemberQaiser_Iftikhar15-Feb-11 13:45 
GeneralRe: Is it possible in Web forms? Pinmemberdeja_anbu15-Feb-11 21:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.1 | Last Updated 27 Jun 2009
Article Copyright 2009 by Qaiser_Iftikhar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid