Click here to Skip to main content
13,300,654 members (54,516 online)
Click here to Skip to main content
Add your own
alternative version


69 bookmarked
Posted 10 Mar 2006

Getting the user idle time with C#

, 10 Mar 2006
Rate this:
Please Sign up or sign in to vote.
Getting the user idle time using C# and the Windows API.

Sample Image


System idle time is the time that has passed since the computer has received its last input from the user. It can be used to detect whether the user is idle or not. This feature is present in many instant messaging systems.

Visual Studio 2003 does not provide an explicit way to accomplish this, but I found an alternate method using C# code only.

Using the code

The implemented solution calls functions inside the user32.dll and kernel32.dll files.

To do this, it is necessary:

  1. To add a reference to the System.Runtime.InteropServices namespace.
  2. To know the name and definition of the function to be called.
  3. To create an extern function using C#.
  4. To make any additional structure to be used in the called functions.
// Step 1
using System.Runtime.InteropServices;

// Steps 2 and 3
private static extern bool 
        GetLastInputInfo(ref LASTINPUTINFO plii);

//Step 4
internal struct LASTINPUTINFO 
    public uint cbSize;

    public uint dwTime;

When I was trying to make things works, I found an issue with the LASTINPUTINFO structure. I solved the problem by creating a C# struct with the same members as the original function. Note that you need to know the mapping between a Windows data type and the .NET Framework data types. The following table is very useful:

BOOLBoolean variable (should be TRUE or FALSE).
BOOLEANBoolean variable (should be TRUE or FALSE).
BYTEByte (8 bits).
CALLBACKCalling convention for callback functions.
CHAR8-bit Windows (ANSI) character.
COLORREFRed, green, blue (RGB) color value (32 bits).
CONSTVariable whose value is to remain constant during execution.
DWORD32-bit unsigned integer.
DWORD_PTRUnsigned long type for pointer precision. Use when casting a pointer to a long type to perform pointer arithmetic. (Also commonly used for general 32-bit parameters that have been extended to 64 bits in 64-bit Windows.)
INT32-bit signed integer.
INT_PTRSigned integral type for pointer precision. Use when casting a pointer to an integer to perform pointer arithmetic.
INT3232-bit signed integer.
INT6464-bit signed integer.
LONG32-bit signed integer.
LONG_PTRSigned long type for pointer precision. Use when casting a pointer to a long to perform pointer arithmetic.
LONG3232-bit signed integer.
LONG6464-bit signed integer.
LONGLONG64-bit signed integer.
LPARAMMessage parameter.
LPBOOLPointer to a BOOL.
LPBYTEPointer to a BYTE.

When using a struct as a parameter, you have to initialize it before it is passed to the function:

lastInPut.cbSize = (uint)

The rest of the code does not contain any trick.

Using the code

The best method to use the code is compiling it in an DLL and using the functions asynchronously using a timer or threads.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Costa Rica Costa Rica
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionReset tick in an event Pin
Member 1232798817-Mar-17 4:44
memberMember 1232798817-Mar-17 4:44 
GeneralMy vote of 5 Pin
Member 856175414-Aug-14 3:17
memberMember 856175414-Aug-14 3:17 
SuggestionGetLastInputInfo additional info Pin
jorge_toriz16-Jun-14 8:39
memberjorge_toriz16-Jun-14 8:39 
BugBUG FOUND & FIXED: Environment.TickCount returns negative value in GetIdleTime() Windows 7 X64 Pin
jeffsdjfhj321-Jun-13 4:01
memberjeffsdjfhj321-Jun-13 4:01 
GeneralRe: BUG FOUND & FIXED: Environment.TickCount returns negative value in GetIdleTime() Windows 7 X64 Pin
sihirbazz10-Jun-14 9:58
membersihirbazz10-Jun-14 9:58 
GeneralRe: BUG FOUND & FIXED: Environment.TickCount returns negative value in GetIdleTime() Windows 7 X64 Pin
Dasiths17-May-15 15:26
memberDasiths17-May-15 15:26 
GeneralMy vote of 4 Pin
Jorge Ali Urbina16-Mar-13 8:36
memberJorge Ali Urbina16-Mar-13 8:36 
GeneralMy vote of 5 Pin
Supriya Srivastav11-Jan-12 22:50
memberSupriya Srivastav11-Jan-12 22:50 
Nice Article...
GeneralMy vote of 5 Pin
andoniu1-Nov-11 22:53
memberandoniu1-Nov-11 22:53 
QuestionWorking in 64 bit Windows Pin
eduardo zola24-Jul-11 16:28
membereduardo zola24-Jul-11 16:28 
GeneralMy vote of 5 Pin
That's Aragon21-Jul-11 3:47
memberThat's Aragon21-Jul-11 3:47 
Generalthx Pin
binabic6-May-11 11:49
memberbinabic6-May-11 11:49 
GeneralMy vote of 5 Pin
Gilberto Alexandre17-Feb-11 0:33
memberGilberto Alexandre17-Feb-11 0:33 
QuestionIs it possible in Web forms? Pin
deja_anbu12-Feb-11 23:31
memberdeja_anbu12-Feb-11 23:31 
GeneralBehave really bad on vista Pin
Sergejack27-Jan-10 6:17
memberSergejack27-Jan-10 6:17 
GeneralRe: Behave really bad on vista Pin
tiuk3-Jun-10 7:33
membertiuk3-Jun-10 7:33 
GeneralGetting the idle time of another system in network Pin
SHRIDHAR TL14-Oct-09 3:37
memberSHRIDHAR TL14-Oct-09 3:37 
GeneralRe: Getting the idle time of another system in network Pin
Andrew Orange17-Dec-09 19:56
memberAndrew Orange17-Dec-09 19:56 
GeneralPretty slick Pin
Mythos724-Aug-09 10:29
memberMythos724-Aug-09 10:29 
GeneralVirtual machine Issues Pin
snorkie16-Apr-09 13:07
membersnorkie16-Apr-09 13:07 
AnswerThis little piece of code saved me this morning Pin
German Sosa11-Apr-09 5:35
memberGerman Sosa11-Apr-09 5:35 
GeneralFantastic Pin
z00z07-Oct-08 8:04
memberz00z07-Oct-08 8:04 
Generalwell done Pin
alexandru robert, agrapine7-Jun-08 0:16
memberalexandru robert, agrapine7-Jun-08 0:16 
Generalabsolutely great! Pin
Luis Miguel Silva16-Feb-08 13:46
memberLuis Miguel Silva16-Feb-08 13:46 
GeneralLicense Pin
Greg Campbell9-Jan-08 13:20
memberGreg Campbell9-Jan-08 13:20 
QuestionGetIdleTime Pin
segusmitha25-Sep-07 3:13
membersegusmitha25-Sep-07 3:13 
AnswerRe: GetIdleTime Pin
Xavi23cr25-Sep-07 4:08
memberXavi23cr25-Sep-07 4:08 
GeneralBless you! Pin
Boaz Cohen19-Aug-07 11:40
memberBoaz Cohen19-Aug-07 11:40 
QuestionTotal idle time? Pin
id10t7-Feb-07 10:28
memberid10t7-Feb-07 10:28 
QuestionHave I misundertood the whole thing? Pin
palei13-Dec-06 3:43
memberpalei13-Dec-06 3:43 
AnswerRe: Have I misundertood the whole thing? Pin
tiuk3-Jun-10 7:34
membertiuk3-Jun-10 7:34 
Generalapplication inactivity Pin
Higgs9619-Sep-06 7:55
memberHiggs9619-Sep-06 7:55 
GeneralApplication Idle Pin
daDude14-Mar-06 8:31
memberdaDude14-Mar-06 8:31 
GeneralRe: Application Idle Pin
daDude14-Mar-06 8:33
memberdaDude14-Mar-06 8:33 
GeneralRe: Application Idle Pin
mwdiablo18-Apr-06 11:46
membermwdiablo18-Apr-06 11:46 
GeneralRe: Application Idle Pin
daDude18-Apr-06 15:54
memberdaDude18-Apr-06 15:54 
GeneralApplication Idle Pin
daDude14-Mar-06 8:31
memberdaDude14-Mar-06 8:31 
QuestionMouse and keyboard? Pin
Drew Noakes14-Mar-06 7:31
memberDrew Noakes14-Mar-06 7:31 
AnswerRe: Mouse and keyboard? Pin
Xavi23cr14-Mar-06 10:09
memberXavi23cr14-Mar-06 10:09 
GeneralOnly Windows 2000 and later Pin
Marqs13-Mar-06 23:10
memberMarqs13-Mar-06 23:10 
Generalvery useful Pin
xsummerx12-Mar-06 0:21
memberxsummerx12-Mar-06 0:21 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 10 Mar 2006
Article Copyright 2006 by Xavi23cr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid