Geekpedia Programming Tutorials






Retrieving the Operating System Idle Time, Uptime and Last Input Time

We are going into Windows's unmanaged user32.dll library in order to retrieve the operating system uptime, last activity time, and user idle time.

On Monday, June 4th 2007 at 08:59 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.8 with 13 votes)
Contextual Ads
More C# Resources
Advertisement
Download this Visual Studio 2005 project Download this project (Visual Studio 2005)

In this C# programming tutorial here at Geekpedia, we're venturing into unmanaged code again. It's the popular user32.dll that has the GetLastInputInfo() function which interests us in this tutorial. The application will work with Windows 2000, XP, Server 2003, Server 2008 and Vista.
Start by creating a form with 3 Labels: lblIdleTime, lblSystemUptime and lblLastInput. Also add a Timer tmrIdle. Set the Interval property to 1000 (1 second) and the Enabled property to True. Every time this timer ticks, we'll check the last input time and the system uptime. Thus setting the timer to tick every 1 second is a pretty good interval for checking this, accurate but not an overkill.

Idle Timer Form

Now let's write some code. Since we're using an unmanaged library, first comes the additional using statement:


using System.Runtime.InteropServices;


Next comes the signature function and the definition of the struct that is passed to it:


// Unmanaged function from user32.dll

[DllImport("user32.dll")]

static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

 

// Struct we'll need to pass to the function

internal struct LASTINPUTINFO

{

    public uint cbSize;

    public uint dwTime;

}


Now it's time to double-click the tmrIdle Timer object so that we get to its Tick event. Here comes the main code, which is self-explanatory thanks to the comments:


private void tmrIdle_Tick(object sender, EventArgs e)

{

    // Get the system uptime

    int systemUptime = Environment.TickCount;

    // The tick at which the last input was recorded

    int LastInputTicks = 0;

    // The number of ticks that passed since last input

    int IdleTicks = 0;

 

    // Set the struct

    LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();

    LastInputInfo.cbSize = (uint)Marshal.SizeOf(LastInputInfo);

    LastInputInfo.dwTime = 0;

 

    // If we have a value from the function

    if (GetLastInputInfo(ref LastInputInfo))

    {

        // Get the number of ticks at the point when the last activity was seen

        LastInputTicks = (int)LastInputInfo.dwTime;

        // Number of idle ticks = system uptime ticks - number of ticks at last input

        IdleTicks = systemUptime - LastInputTicks;

    }

 

    // Set the labels; divide by 1000 to transform the milliseconds to seconds

    lblSystemUptime.Text = Convert.ToString(systemUptime / 1000) + " seconds";

    lblIdleTime.Text = Convert.ToString(IdleTicks / 1000) + " seconds";

    lblLastInput.Text = "At second " + Convert.ToString(LastInputTicks / 1000);

}


Retrieving the operating system uptime is done through managed code (Environment.TickCount), however the last activity time is retrieved through the unmanaged GetLastInputInfo() function that we prepared earlier. From then on, calculating the last input ticks is simple math: subtract the last activity milliseconds from the system uptime and there you have it.

Idle Timer
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Yaniv on Wednesday, June 6th 2007 at 03:10 PM

Is there a way to get the idle time of a computer over the network?

by Andrei Pociu on Wednesday, June 6th 2007 at 04:11 PM

Not unless you write an application using this code and run it as a service on the network computer.

by yaniv on Thursday, June 7th 2007 at 02:26 PM

WIll you be kind to give me some advice how to write that service?
or how to run it as service?

by bahram on Monday, June 11th 2007 at 12:10 PM

by Kenji on Tuesday, June 12th 2007 at 08:48 PM

I am trying to learn how to do this for a long time and you certainly help me alot. I think my project can get extra marks now. =D

by Grant on Wednesday, June 11th 2008 at 10:31 AM

I keep getting a System.Security.Permissions.SecurityPermission error message. Anyone have any ideas why this may be?

by Jagat on Thursday, June 12th 2008 at 02:33 AM

this code is really marvelleous....

thanks dude.

by Venkat on Friday, July 4th 2008 at 07:19 AM

This is really a good work. Idle time is calculated based on Last Input.If any backgroud process like file copy or download runs,how we can track that also? - Thanks in advance.

by fvgsdgv on Wednesday, December 10th 2008 at 08:09 AM

gfdgsd

by Jaydel on Thursday, January 1st 2009 at 11:17 AM

This is really good code but I noticed that if a computer is up for 25 days or more the numbers are not correct.

Environment.TickCount -1791175703 int

After 25 days the tickcount goes to the smallest number and then starts going back to 0

by souvik on Friday, January 23rd 2009 at 04:15 AM

The concept is very fine but I work on Dreamwever
with IIs support How can I run this code?
bcoz provided code is not running in my environment

by souvik on Friday, January 23rd 2009 at 04:15 AM

The concept is very fine but I work on Dreamwever
with IIs support How can I run this code?
bcoz provided code is not running in my environment

by souvik on Friday, January 23rd 2009 at 04:15 AM

The concept is very fine but I work on Dreamwever
with IIs support How can I run this code?
bcoz provided code is not running in my environment

by souvik on Friday, January 23rd 2009 at 04:16 AM

The concept is very fine but I work on Dreamwever
with IIs support How can I run this code?
bcoz provided code is not running in my environment

by souvik on Friday, January 23rd 2009 at 04:16 AM

The concept is very fine but I work on Dreamwever
with IIs support How can I run this code?
bcoz provided code is not running in my environment

by Bad programmer on Sunday, April 19th 2009 at 06:37 AM

Very good one.. thanks


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons