获取系统闲置时间

 
http://www.pinvoke.net/default.aspx/user32.GetLastInputInfo
[StructLayout(LayoutKind.Sequential)]
public struct LASTINPUTINFO
{
  [MarshalAs(UnmanagedType.U4)]
  public int cbSize;
  [MarshalAs(UnmanagedType.U4)]
  public uint dwTime;
}

static class NativeMethods
{
   /// <summary>
   /// 获取上一次输入的时间。
   /// </summary>
   /// <param name="plii"></param>
   /// <returns>true:获取成功。false:获取失败。</returns>
   [DllImport("user32.dll")]
   public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
   /// <summary>
   /// 获取上一次操作后的闲置时间。
   /// </summary>
   /// <returns>闲置时间的毫秒数。</returns>
    public static long GetIdleTick()
    {
         LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
         vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
         if (!GetLastInputInfo(ref vLastInputInfo)) return 0;
         return Environment.TickCount - (long)vLastInputInfo.dwTime;
    }
}
posted on 2012-08-27 14:17  Peter Zhang  阅读(712)  评论(0编辑  收藏  举报