博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
系统空闲时间 解决 GetLastInputInfo 负数问题
阅读量:7112 次
发布时间:2019-06-28

本文共 1689 字,大约阅读时间需要 5 分钟。

using System;

using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ATMDefendCabinEventVideoForm

{
public class CheckComputerFreeState
{

/// <summary>

/// 创建结构体用于返回捕获时间
/// </summary>
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
/// <summary>
/// 设置结构体块容量
/// </summary>
[MarshalAs(UnmanagedType.U4)]
public int cbSize;

/// <summary>

/// 抓获的时间
/// </summary>
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}

[DllImport("user32.dll")]

private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
/// <summary>
/// 获取键盘和鼠标没有操作的时间
/// </summary>
/// <returns>用户上次使用系统到现在的时间间隔,单位为秒</returns>
public static long GetLastInputTime()
{
LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
if (!GetLastInputInfo(ref vLastInputInfo))
{
return 0;
}
else
{
Console.WriteLine("TickCount=" + Environment.TickCount);
Console.WriteLine("vLastInputInfo.dwTime=" + vLastInputInfo.dwTime);
Console.WriteLine("vLastInputInfo.dwTime & Int32.MaxValue=" + (vLastInputInfo.dwTime & Int32.MaxValue));
var count = Environment.TickCount & Int32.MaxValue - (long)(vLastInputInfo.dwTime & Int32.MaxValue);
var icount = count / 1000;
return icount;
}
}

public static long GetLastInputTimeMinute()
{
LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
if (!GetLastInputInfo(ref vLastInputInfo))
{
return 0;
}
else
{
var count = Environment.TickCount - (long)vLastInputInfo.dwTime;
var icount = count / (1000*60);
return icount;
}
}

}

}

转载于:https://www.cnblogs.com/bkyrslf/p/9451212.html

你可能感兴趣的文章
git团队开发流程
查看>>
【Under-the-hood-ReactJS-Part6】React源码解读
查看>>
深入理解css之vertical-align
查看>>
Laravel事件
查看>>
matlab绘制peano(皮亚诺)曲线和koch(科赫曲线,雪花曲线)分形曲线
查看>>
使用pipenv代替virtualenv管理python包
查看>>
Docker零基础入门指南(四):Docker容器使用
查看>>
React 深入系列4:组件的生命周期
查看>>
Mybatis之设计模式之迭代器模式
查看>>
房间号生成器
查看>>
CentOS 6.8 安装vsftpd
查看>>
js设计模式 --- 装饰设计模式
查看>>
Flask源代码阅读笔记(一)——应用启动
查看>>
IOS精品源码,仿探探UIButton封装iOS提示弹框迅速引导页自定义导航栏
查看>>
setState的一个Synthetic Event Warning
查看>>
通读Python官方文档之wsgiref(未完成)
查看>>
2017回顾
查看>>
Maven3 快速入门
查看>>
《编写可读代码的艺术》——表面层次的改进
查看>>
RxJS Observable - 一个奇特的函数
查看>>