Click here to Skip to main content
13,146,739 members (90,514 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 5 Oct 2004

RtwIdleDll Control

, 5 Oct 2004
Rate this:
Please Sign up or sign in to vote.
DLL to track the system idle time.

Sample Image - rtwidledll.jpg


The 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 (like they have done in Yahoo! Messenger, when there is no activity for a certain amount of time the status of the user is shown as idle). This has many other applications as well. The Application idle time (not the system idle time) can be found easily by resetting a timer on every keyboard or mouse event, but in most cases, it is not adequate for our purposes. Unfortunately, the .NET framework class libraries don't provide much support for getting the system idle time directly.

To do this, there are several ways. One way is by hooking into the mouse and keyboard events of the OS. But I believe the easiest way is by using the GetLastInputInfo() Win32 API. I tried using this in C# without success. So, I decided to write a UserControl in C++ that can be used easily in C# or other languages.

DLL Description

The core of the DLL is the two methods GetLastInputTime() and GetIdleTime(). They use the functions GetLastInputInfo() and GetTickCount() defined in windows.h to get the time from system startup to the last user input and the total time passed since system startup, respectively. Please refer the MSDN documentation for more info on GetLastInputInfo() and GetTickCount().

<PRE lang=mc++>//returns the time from system startup to last input in milliseconds DWORD RtwIdleDll::RtwIdleDllControl::GetLastInputTime(void) { LASTINPUTINFO lastInput; lastInput.cbSize = sizeof(LASTINPUTINFO); BOOL success = GetLastInputInfo(&lastInput); if(!success) { DWORD err=GetLastError(); // report error, throw exception, etc } DWORD lastInputTime = lastInput.dwTime; return lastInputTime; }<PRE lang=mc++>//returns the time since the last input in milliseconds DWORD RtwIdleDll::RtwIdleDllControl::GetIdleTime(void) { DWORD totalTime = GetTickCount(); //gets the time from system startup to now in milliseconds DWORD lastInputTime = GetLastInputTime(); DWORD idleTime = totalTime - lastInputTime; return idleTime; }

The following section lists the properties, methods and events exposed by the DLL:


  • IdleTriggerTime (get/set) - The time to wait (in milliseconds) before the first Idle and IdleStart events are fired.
  • IsIdle (get) - Is true when idle, otherwise false.


  • ShowAbout() - Shows the About box.


  • IdleStart - Fired one time when the idle trigger time is passed.
  • Idle - Fired continuously when idle.
  • IdleStop - Fired when idle state ends (keyboard or mouse input received).


The events are not asynchronous, therefore, using code that would delay the client event handler methods from returning (like modal dialogs or message boxes) inside the event handler methods will sometimes cause unexpected behavior.

Using the DLL

The DLL must be added to another project through the ToolBox. If you are using Visual Studio .NET, right click on the ToolBox and select "Add/Remove Items..." from the popup menu and select the DLL from its location. After that, it will appear in your ToolBox. Then it can be drag'n dropped to any container at design time. After setting the properties and events, it's ready for use.


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


About the Author

Rajitha Wimalasooriya
Software Developer (Senior)
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

Generalnot a milliseconds Pin
masaniparesh20-Nov-08 19:36
membermasaniparesh20-Nov-08 19:36 
GeneralRe: not a milliseconds Pin
masaniparesh20-Nov-08 19:36
membermasaniparesh20-Nov-08 19:36 
GeneralGetLastInputInfo, Pin
masaniparesh19-Nov-08 23:36
membermasaniparesh19-Nov-08 23:36 
Questionmilliseconds??? Pin
masaniparesh19-Nov-08 22:51
membermasaniparesh19-Nov-08 22:51 
Generalfails to build with 2005 Express Pin
SysyphusJones16-Feb-07 7:18
memberSysyphusJones16-Feb-07 7:18 
GeneralFor Windows 95/98/Me Support Pin
Paul Kissel21-Oct-04 8:20
memberPaul Kissel21-Oct-04 8:20 
GeneralMore limitations Pin
Neville Franks7-Oct-04 10:00
memberNeville Franks7-Oct-04 10:00 
GeneralDllImport Pin
mteinum6-Oct-04 9:57
membermteinum6-Oct-04 9:57 
GeneralRe: DllImport Pin
Rajitha Wimalasooriya6-Oct-04 19:02
memberRajitha Wimalasooriya6-Oct-04 19:02 

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
Web03 | 2.8.170915.1 | Last Updated 5 Oct 2004
Article Copyright 2004 by Rajitha Wimalasooriya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid