Monday, August 01, 2011

How to find the system idle process using C#.net

Posted by Rahul Kharde at 10:03 PM
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);
}


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

2 comments:

samar gul on March 7, 2016 at 1:20 AM said...

thanks bro it working fine


samar gul on March 7, 2016 at 1:21 AM said...

thanks bro it working fine


Have any question? Feel Free To Post Below:

 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top