views:

218

answers:

1

What's the simplest way of programatically checking whether or not a particular Windows machine is currently being used by a human being?

I'm thinking about writing a "politeness monitor" for our rather limited Internet connection - basically a way to see who is currently actively using the Internet at any one time, so when someone needs to download a large file, they can minimise the annoyance to others (and keep those statistics historically, so one can predict when the uplink isn't going to be too laggy). I'm trying to gather multiple sources of evidence which one could then use to infer activity.

For Windows users, it would be quite useful to be able to see whether or not they are typing or moving the mouse around, or some other equivalent measure of activity. I've already considered using the System.Diagnostics.Process class to see if certain processes are running (online games and other bandwidth heavy apps) and when they were launched. I figure if any of a bunch of common userland processes (firefox, iTunes etc.) were launched in the last half-hour, it's a reasonable bet that there's a meatbag sitting in the chair doing something. Once I've detected activity, I send a tiny HTTP POST request (saying "Hey, I'm alive and doing something!") to the server and a webapp running on my Linux box gets updated to show that the user is active.

I've considered using IM status, but different IM clients seem to have different levels of OS integration - and people don't seem to update those as much as would be nice. Like all good Web 2.0 citizens, I am using Twitter, of course. ;)

Disclaimer: I'm a UNIX and Mac guy who avoids Windows. I've fiddled around with .NET in Mono but have never touched Win32 APIs before. Be gentle.

+2  A: 

You might want to look at GetLastInputInfo:

The GetLastInputInfo function retrieves the time of the last input event.

It's a Windows API function you'd call via interop:

[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

[StructLayout( LayoutKind.Sequential )]
struct LASTINPUTINFO
{
    public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));

    [MarshalAs(UnmanagedType.U4)]
    public int cbSize;    
    [MarshalAs(UnmanagedType.U4)]
    public UInt32 dwTime;
}

Edit: er, you did't specify in the question what language you're using, I just assumed .Net based since you referenced System.Diagonstics.Process

Factor Mystic
Thanks. Either C# or a mixture of C# and IronRuby.
Tom Morris