1

I am looking for a program that measures system idle time on Windows. I have found many codes that do this. For example,

http://www.codeproject.com/KB/cs/GetIdleTimeWithCS.aspx

However, I also want to take into account user watching movies, videos, etc. That time no input is given, but still the system is not idle.

Is there anyway to do this?

Shashank Jain
  • 847
  • 2
  • 10
  • 25
  • Your only option is connecting to API for each of the programs you don't consider idle and check if its playing something. – Daniel Aug 04 '11 at 13:21
  • You should start by coming up with a objective definition of what you mean by idle time. – hammar Aug 04 '11 at 13:28
  • I just want to perform a task, like starting and monitoring a wabcam, when user has not used system for certain amount of time. – Shashank Jain Aug 04 '11 at 13:34
  • Are you trying to do this your self or just looking for an app that already does this? If you just want an app you could look at rescuetime http://www.hanselman.com/blog/ProductivityAndContinuousImprovementMeasurementAndRescueTimeMakesItHappenBothPersonallyAndAtWork.aspx – Chris Chilvers Aug 04 '11 at 13:58

3 Answers3

1

This function detects if a process is running in fullscreen in forground, and returns name of the process if found so:

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [DllImport("user32.dll")]
    private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [DllImport("user32.dll")]
    private static extern bool
    GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

    private struct POINTAPI
    {
        public int x;
        public int y;
    }

    private struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    private struct WINDOWPLACEMENT
    {
        public int length;
        public int flags;
        public int showCmd;
        public POINTAPI ptMinPosition;
        public POINTAPI ptMaxPosition;
        public RECT rcNormalPosition;
    }

    public string FullscreenProcess()
    {
        IntPtr foreWindow = GetForegroundWindow();

        // get the placement
        WINDOWPLACEMENT forePlacement = new WINDOWPLACEMENT();
        forePlacement.length = Marshal.SizeOf(forePlacement);
        GetWindowPlacement(foreWindow, ref forePlacement);

        if (forePlacement.rcNormalPosition.top == 0 && forePlacement.rcNormalPosition.left == 0 && forePlacement.rcNormalPosition.right == Screen.PrimaryScreen.Bounds.Width && forePlacement.rcNormalPosition.bottom == Screen.PrimaryScreen.Bounds.Height)
        {
            uint processID;
            GetWindowThreadProcessId(foreWindow, out processID);
            Process proc = Process.GetProcessById((int)processID);

            return proc.ProcessName;
        }
    return null;
    }

After this, we just need to match the returned process name with a set of popular media players or other processes.

Limitation is that we have assumed user plays in fullscreen.

Shashank Jain
  • 847
  • 2
  • 10
  • 25
0

You can get the idle time for computer using this, however your problem is how to know weather a movie is playing, you can check the processes that is currently running on the computer System.Diagnostics.Process.GetProcesses(), and then check if one of them is a known movie player. However I don't think this will achieve what you locking for either, because even if you find out that for example a Gom player is running, you still don't know if it running a video or audio or just opened without any thing actually playing...

Community
  • 1
  • 1
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
  • 1
    That's right. There must be way to do this through API, because windows itself is able to detect the time (because computer screen doesn't go off if you are watching movies). – Shashank Jain Aug 04 '11 at 13:42
  • In some movie players the screen sever will work even if movie are playing, so many of movie player add an option to suppress the screen sever while playing. – Jalal Said Aug 04 '11 at 13:46
  • @Shashank: for instance Gom player provides a check box option `Disaple screensever during playback`, so the movie player itself prevents the scrensever maybe through some api. – Jalal Said Aug 04 '11 at 13:49
  • I would also say that it isn't windows who detects it. If i would program a player i would add an option to disable the screensaver while the move is running... – trampi Aug 04 '11 at 13:49
  • Even if it is done by media players, how do they do it? If they are resetting some variable, we may be able to detect that. – Shashank Jain Aug 04 '11 at 13:56
  • That is another question, however how knows, they maybe just change the registry to disable the screensaver "[check this](http://zayko.net/post/C-How-To-Disable-Windows-Screensaver-programmatically.aspx)", and when closing just restore that. – Jalal Said Aug 04 '11 at 14:03
0

In case you watch videos in fullscreen: You could use code like this do determine if there is an app running in fullscreen:

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
struct RECT
{
    public int Left;
    public int Top;
    public int Width;
    public int Height;
}

static bool IsBigWindowRunning()
{
    foreach (Process proc in Process.GetProcesses())
    {
        RECT rect;
        var success = GetWindowRect(proc.MainWindowHandle, out rect);
        if(success && (r.Left + r.Width) >= Screen.PrimaryScreen.WorkingArea.Width)
        {
            return true;
        }
    }

    return false;
}

I haven't tested it well and i would definetely play around with this line, add some limits:

if((r.Left + r.Width) >= Screen.PrimaryScreen.WorkingArea.Width)

Good luck!

trampi
  • 2,214
  • 4
  • 17
  • 17
  • 1
    Nice suggestion. Let me try it out too. Will share my experience here. – Shashank Jain Aug 04 '11 at 14:12
  • 1
    If this programs detects fullscreen app correctly, we may get the answer. If an app is running in fullscreen, we get its process name and match with names of popular media players. If a match is found, it means a media player is playing a video in fullscreen. So, to a lot extent, it solves the problem. – Shashank Jain Aug 04 '11 at 14:32