To get idle use LASTINPUTINFO method. Here is
the complete working sample code.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinApp1
{
   
public partial class Form3 : Form
   
{
       
LASTINPUTINFO lastInputInf = new LASTINPUTINFO();
       
[DllImport("user32.dll")]
       
public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
       
[StructLayout(LayoutKind.Sequential)]
       
public struct LASTINPUTINFO
       
{
            [MarshalAs(UnmanagedType.U4)]
      
     public int cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public int dwTime;
       
}
       
public Form3()
       
{
            InitializeComponent();
       
}
       
private void Form3_Load(object sender, EventArgs e)
       
{
            Timer dt = new Timer();
            dt.Tick += Timer_Tick;
            dt.Interval = 1000;
            dt.Start();
       
}
       
public void Timer_Tick(object sender, EventArgs e)
       
{
            DisplayIdleTime();
       
}
       
public int GetIdleTime()
       
{
            int idletime = 0;
            idletime = 0;
            lastInputInf.cbSize = Marshal.SizeOf(lastInputInf);
            lastInputInf.dwTime = 0;
            if (GetLastInputInfo(ref lastInputInf))
            {
                idletime = Environment.TickCount -
lastInputInf.dwTime;
            }
            if (idletime != 0)
            {
                return idletime / 1000;
            }
            else
            {
                return 0;
            }
       
} 
       
private void DisplayIdleTime()
       
{
                label1.Text = "Idle time =
" + " " +
GetIdleTime().ToString() + " " + "seconds";
       
}
   
}
}
 
No comments:
Post a Comment