C#获取键盘和鼠标操作的时间的类

///



/// 创建结构体用于返回捕获时间

///

[StructLayout(LayoutKind.Sequential)]

struct LASTINPUTINFO

{

///



/// 设置结构体块容量

///

[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
{
long count = Environment.TickCount - (long)vLastInputInfo.dwTime;
//long icount = count / 1000;
return count;
}
}

private void timer1_Tick(object sender, EventArgs e)

{

int sunNumber=int.Parse(GetLastInputTime().ToString());

if (sunNumber >= 30000)

{

this.Close();

}

    }
上一篇:MySQL中同时存在创建和更新时间戳字段解决方法浅析


下一篇:SELinux导致无法访问外网,PHP连接MySQL异常Can't connect to MySQL server、redis程序访问提示Redis server went away的解决方法