S h o r t S t o r i e s

// Tales from software development

How long has my computer been idle ?

leave a comment »

I have a few applications that periodically perform disk intensive tasks. For example, one checks for new or changed files in the folders that I normally backup. If the number of new or changed files, or the total size of these files, exceeds a threshold then the application starts nagging me to run a backup.

These tasks run with a very low thread priority and any performance impact is usually unnoticable. Nevertheless, it’d be nice to change these tasks so that they run when the computer is not being used. I looked through the .NET framework for methods that might help me determine the current level of activity on the computer but didn’t find anything paritcularly relevant.

Using this code I can now change my tasks to check if the computer is being used before they perform their disk intensive processing.

Then I stumbled across the GetLastInfo function in the Win32 API. This obscure function returns a single value that represents the time that the user last interacted with the computer using the keyboard or mouse. The value is expressed in ticks. This is the number of milliseconds that the Windows has been running since it was started. The .NET Framework Environment class has a static property called TickCount() that returns the current tick value. So, subtracting the value returned by GetLastInfo() from Environment.TickCount gives the number of millseconds since the user last interacted with the computer. For example:

namespace IdleTime
{
    using System;
    using System.Runtime.InteropServices;  

    public static class IdleTime
    {
        [DllImport("user32.dll")]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

        private struct LASTINPUTINFO
        {
            public uint cbSize;
            public uint dwTime;
        }
 
        public static int Get()
        {
            int idleSeconds = 0;
 
            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
                int lastInputTicks = (int)LastInputInfo.dwTime;
 
                // Number of idle ticks = system uptime ticks - number of ticks at last input
                idleSeconds = (Environment.TickCount - lastInputTicks) / 1000;
            }

            return idleSeconds;
        }
    }
}

Using this code I can now change my tasks to check if the computer is being used before they perform their disk intensive processing.

Advertisements

Written by Sea Monkey

April 23, 2009 at 8:00 pm

Posted in Development

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: