Follow these steps:
1- Add a Timer
to your Form
.
2- Set its interval property to 1000 (set it in form_load
or in design mode from Properties window).
3-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);
}
4- in Form_Load
start the timer:
timer1.Start();
5- in timer tick
event check GetIdleTime()
, for example if it is greater than 5000
means application was idled since 5 seconds ago.
private void timer1_Tick(object sender, EventArgs e)
{
if (GetIdleTime() > 5000)
Application.Exit();//For Example
}