C#代码中Windows系统空闲时间计算和总空闲时间?

问题描述 投票:0回答:1

为用户活动流程开发Windows应用程序。需要使用 C# 应用程序计算用户没有使用系统的时间以及使用系统的时间有多少时间来提高工作性能。我正在使用遵循代码,但没有收到正确的空闲时间和持续时间值。谁能帮我解决这个问题。

       public class clsAttendance
       {        
        int _IdleCount = 0;
       

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

        public clsAttendance()
        {
            _clsData = new clsDataConnection();
        }    


        public uint GetIdleTime()
        {
            LASTINPUTINFO lastInPut = new LASTINPUTINFO();
            lastInPut.cbSize = 
        (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
            GetLastInputInfo(ref lastInPut);

            return ((uint)Environment.TickCount - lastInPut.dwTime);
        }

        public uint GetLastInputTime()
        {
            uint idleTime = 0;
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            uint envTicks = (uint)Environment.TickCount;

            if (GetLastInputInfo(ref lastInputInfo))
            {
                uint lastInputTick = lastInputInfo.dwTime;

                idleTime = envTicks - lastInputTick;
            }

            return ((idleTime > 0) ? (idleTime / 1000) : 0);
        }
               
    }

    internal struct LASTINPUTINFO
    {
        public uint cbSize;

        public uint dwTime;
    }


private void Form1_Load(object sender, EventArgs e)
        {
     a = new Timer();
         a.Tick += A_Tick;
         a.Interval = 10 * 60 * 1000;
         a.Start();

    }

 private void A_Tick(object sender, EventArgs e)
        {         

            ExceptionLogger.WriteErrorLog($"Current time: {DateTimeOffset.Now}");

            ExceptionLogger.WriteErrorLog($"Last input time without timespan: {  clsSystemIdle.GetLastInputTime()}");

            ExceptionLogger.WriteErrorLog($"Idle time without timespan: {_clsAtt.GetIdleTime()}");

            ExceptionLogger.WriteErrorLog(Message: $"Last Input TimeSpan Minutes : {TimeSpan.FromMilliseconds(clsSystemIdle.GetLastInputTime())}");

            ExceptionLogger.WriteErrorLog(Message: $"Idle TimeSpan Minutes : {TimeSpan.FromMilliseconds(_clsAtt.GetIdleTime())}");

        }

过去 40 分钟没有使用键盘和鼠标,然后返回以下输出,

当前时间:3/4/2022 1:49:59 PM +05:30 最后输入时间(无时间跨度):125 无时间跨度的空闲时间:125266 最后输入时间跨度:00:00:00.1250000 空闲时间跨度:00:02:05.3440000

当前时间:3/4/2022 1:59:59 PM +05:30 最后输入时间(无时间跨度):22 无时间跨度的空闲时间:22203 最后输入时间跨度:00:00:00.0220000 空闲时间跨度:00:00:22.2810000

当前时间:3/4/2022 2:09:59 PM +05:30 上次输入时间(无时间跨度):101 无时间跨度的空闲时间:101329 最后输入时间跨度:00:00:00.1010000 空闲时间跨度:00:01:41.4380000

当前时间:3/4/2022 2:19:59 PM +05:30 无时间跨度的最后输入时间:3 无时间跨度的空闲时间:3172 最后输入时间跨度:00:00:00.0030000 空闲时间跨度:00:00:03.1720000

这里我想要最后 40 分钟的系统空闲,但是如何从上面的返回值中获取 40 分钟的值并打印异常日志文件。请告诉我如何解决这个任务。

c# c#-4.0 desktop-application
1个回答
0
投票

首先,您有两种方法,

GetIdleTime
GetLastInputTime
。它们几乎相同,只是前者以毫秒为单位返回空闲时间,而后者以秒为单位返回
idleTime / 1000
。您只需要以下内容即可获得空闲时间。

public class SomeClass
{
    public static uint GetIdleTime()
    {
        var info = new LASTINPUTINFO();

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

        return GetLastInputInfo(ref info)
            ? (uint)Environment.TickCount - info.dwTime
            : 0;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct LASTINPUTINFO
    {
        [MarshalAs(UnmanagedType.U4)]
        public uint cbSize;
        [MarshalAs(UnmanagedType.U4)]
        public uint dwTime;
    }

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

第二,你说过:

过去 40 分钟没有使用键盘和鼠标,然后返回以下输出...

从您的测试结果来看,这是不准确的,因为空闲时间没有按应有的方式增加。 (分别为 125266、22203、101329 和 3172)这意味着输入设备或代码在该持续时间内多次打破用户空闲状态。否则,

GetIdleTime
方法应返回 2,400,000 毫秒(40 分钟)。

第三,考虑以下代码片段。

// Get and print the idle time in milliseconds...
var it = SomeClass.GetIdleTime();
Console.WriteLine(it);

// Create and print a TimeSpan...
var itTimeSpan = TimeSpan.FromMilliseconds(it)
Console.WriteLine(itTimeSpan.ToString("hh\\:mm\\:ss"));

// Get and print the last input DateTime...
var lastInputTime = DateTime.Now.AddMilliseconds(-it); // Or DateTimeOffset...
Console.WriteLine(lastInputTime);
// Or
Console.WriteLine(lastInputTime.ToString("hh:mm:ss"));
© www.soinside.com 2019 - 2024. All rights reserved.