1- Add a Timer to your Form.
2-Add this method to your Form class.
public static uint GetIdleTime()
{
LASTINPUTINFO LastUserAction = new LASTINPUTINFO();
LastUserAction.cbSize =
(uint)System.Runtime.InteropServices.Marshal.SizeOf(LastUserAction);
GetLastInputInfo(ref LastUserAction);
return ((uint)Environment.TickCount - LastUserAction.dwTime);
}
3- in Form_Load start the timer:
timer1.Start();
4- in timer tick event check GetIdleTime
private void timer1_Tick(object sender, EventArgs e)
{
if (GetIdleTime() > 1200000 )
Application.Exit();
}
<pre lang="c#">using System.Runtime.InteropServices;
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}