如何在C#中测量系统空闲时间,不包括看电影等?

时间:2011-08-04 13:19:11

标签: c# .net windows

我正在寻找一个测量Windows上系统空闲时间的程序。我发现有很多代码可以做到这一点。例如,

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

但是,我也想考虑用户观看电影,视频等。那时候没有给出任何输入,但系统仍然没有空闲。

有没有这样做?

3 个答案:

答案 0 :(得分:1)

此函数检测进程是否在forground中全屏运行,如果找到则返回进程名称:

    [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;
    }

在此之后,我们只需要将返回的流程名称与一组流行的媒体播放器或其他流程进行匹配。

限制是我们假设用户全屏播放。

答案 1 :(得分:0)

您可以使用this获取计算机的空闲时间,但问题是如何了解正在播放的电影的天气,您可以检查当前在计算机上运行的进程System.Diagnostics.Process.GetProcesses(),以及然后检查其中一个是否是已知的电影播放器​​。但是我不认为这会实现你锁定的任何一个,因为即使你发现例如Gom播放器正在运行,你仍然不知道它是运行视频或音频还是只是打开没有任何东西实际上打...

答案 2 :(得分:0)

如果您以全屏模式观看视频: 您可以使用这样的代码来确定是否有全屏运行的应用程序:

[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;
}

我没有对它进行过良好的测试,我会明确地使用这条线,添加一些限制:

if((r.Left + r.Width) >= Screen.PrimaryScreen.WorkingArea.Width)
祝你好运!