Hello Friends,
Follow coding is C#.Net for windows
application to find whether the system is idle or not (i.e. no movement in
mouse or not key press in keyboard). If the changes occurred the time should
refresh and start again from zero.
It's the popular user32.dll
that has the GetLastInputInfo() function which interests us in
this tutorial. The application will work with Windows 2000, XP, Server 2003,
Server 2008 and Vista.
Start by creating a form with Label and Timer. Set the Interval property to 1000 (1 second). Every time this timer ticks, we'll check the last input time and the system uptime. Thus setting the timer to tick every 1 second is a pretty good interval for checking this, accurate but not an overkill.
Code:
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern bool
GetLastInputInfo(ref LASTINPUTINFO
_ObjLastInputInfo);
internal struct LASTINPUTINFO
{
public uint Size;
public uint Time;
}
private void timer1_Tick(object
sender, EventArgs e)
{
int
ObjsysMouseUptime = Environment.TickCount;
int
ObjLastTicks = 0;
int
IdleTicks = 0;
LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
LastInputInfo.Size = (uint)Marshal.SizeOf(LastInputInfo);
LastInputInfo.Time = 0;
if
(GetLastInputInfo(ref LastInputInfo))
{
ObjLastTicks = (int)LastInputInfo.Time;
IdleTicks = ObjsysMouseUptime -
ObjLastTicks;
}
lblTime.Text = Convert.ToString(IdleTicks /
1000);
}
thanks bro it working fine
ReplyDeletethanks bro it working fine
ReplyDelete