Wednesday, September 8, 2010

How to lock the system when it is an Idle on specific interval using C#.Net

using System.Diagnostics;
using System.Runtime.InteropServices;


[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}



private void timer1_Tick(object sender, EventArgs e)
{
int systemUptime = Environment.TickCount;
int LastInputTicks = 0;
int IdleTicks = 0;

LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
LastInputInfo.cbSize = (uint)Marshal.SizeOf(LastInputInfo);
LastInputInfo.dwTime = 0;

if (GetLastInputInfo(ref LastInputInfo))
{
LastInputTicks = (int)LastInputInfo.dwTime;
IdleTicks = systemUptime - LastInputTicks;
}

long idlesec=IdleTicks / 1000;
long usersec = 1 * 60;//user defined values
if (idlesec == usersec)
Process.Start(Environment.SystemDirectory.ToString()+"\\rundll32.exe", "user32.dll,LockWorkStation");
}

No comments:

 
Feedback Form