c# - GetlastInput 和 Tickcount

标签 c#

我有一个控制台应用程序的以下代码片段,如果空闲时间超过 5 秒,我想锁定我的计算机。

问题:5 秒后没有任何反应

    public static 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 static long GetTickCount()
    {
        return Environment.TickCount;
    }

    public static long GetLastInputTime()
    {
        LASTINPUTINFO lastInPut = new LASTINPUTINFO();
        lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
        if (!GetLastInputInfo(ref lastInPut))
        {
            throw new Exception(GetLastError().ToString());
        }
        return lastInPut.dwTime;
    }


private static Timer timer;
    private static uint idle = 0;
    static void Main(string[] args)
    {
        timer = new Timer();
        timer.Enabled = true;
        timer.Interval = 1000;
        timer.Start();

        idle += GetIdleTime();            
        System.Threading.Thread.Sleep(6000);
        if (idle > 5000)
        {
            LockWorkStation();
        }
    }

最佳答案

您的代码有一两个奇怪的地方。 计时器没有做太多事情,您正在累积空闲时间,并且在计算完后 6 秒测试空闲时间,而无需重新计算。

试试这个:

    public static 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 static long GetTickCount()
    {
        return Environment.TickCount;
    }

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

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

    [DllImport("user32.dll")]
    static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool LockWorkStation();

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

        int envTicks = Environment.TickCount;

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

            idleTime = envTicks - lastInputTick;
        }

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


    private static uint idle = 0;
    static void Main(string[] args)
    {
        while (!Console.KeyAvailable)
        {
            System.Threading.Thread.Sleep(500);
            idle = GetIdleTime();
            Console.WriteLine(idle);
            if (idle > 5000)
            {
                LockWorkStation();
            }
        }
    }

关于c# - GetlastInput 和 Tickcount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10977149/

相关文章:

c# - 自动属性的 Fluent NHibernate PropertyNotFoundException

c# - 减去 HashSet(并返回一个副本)?

c# - Azure Cosmos 数据库集合未获取分区键

c# - 使用哪种方式使用 azure 函数将消息发送到主题?

c# - SQL Server 插入

c# - 无需安装即可在网络驱动器上运行的点网应用程序

c# - 如何在内联asp代码中使用内部类

c# - 是 Thread.Sleep(Timeout.Infinite);比 while(true){} 更有效?

c# - Quartz.net 3.0.6 与 system.data.sqlite 1.0.109,没有提供程序 "SQLite-10"的元数据信息

c# - 定位 UnhandledException 的来源