我想在我的程序中加入一个空闲检测机制.就像QQ的,当无键盘鼠标动作时,自动转换成离线状态.如果程序设计为空闲多久就执行某个操作 .
请提供详细代码;或附说明;
提供网页也行。

解决方案 »

  1.   

    用个timer计时,当有操作时就清一次timer,没有的话,当定时器到后,就改变软件的状态呀。
      

  2.   

    写个线程定时探测cpu的使用率,低的话就表示空闲~
      

  3.   

    using System.Runtime.InteropServices;[StructLayout(LayoutKind.Sequential)]
    struct LASTINPUTINFO
    {
        [MarshalAs(UnmanagedType.U4)]
        public int cbSize;
        [MarshalAs(UnmanagedType.U4)]
        public uint dwTime;
    }[DllImport("user32.dll")]
    static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);static long GetLastInputTime()
    {
        LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
        vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
        if (!GetLastInputInfo(ref vLastInputInfo)) return 0;
        return Environment.TickCount - (long)vLastInputInfo.dwTime;
    }private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }private void timer1_Tick(object sender, EventArgs e)
    {
        Text = string.Format("用户已经{0}秒没有路过了", GetLastInputTime());
    }