How can I get the time a user has been idle (i.e didn't perform any input) system wide? Similar like the screensaver or Windows Live Messenger does it. Even better would be to get notified via an event when the idle time has exceeded a specific timespan. Maybe there is something for that in WMI ?
You can P/Invoke the GetLastInputInfo() API function. Check this thread for code to detect the screen saver turning on. Here's a sample app that demonstrates the API function:
using System; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices;
namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); timer1.Interval = 1000; timer1.Tick += CheckLastInput; timer1.Enabled = true; } private void CheckLastInput(object sender, EventArgs e) { LastInputInfo info = new LastInputInfo(); info.cbSize = Marshal.SizeOf(info); GetLastInputInfo(ref info); uint tick = GetTickCount(); uint msec = tick - info.dwTime; Console.WriteLine("Last input was {0} milliseconds ago", msec); } [StructLayout(LayoutKind.Sequential)] private struct LastInputInfo { public int cbSize; public uint dwTime; } [DllImport("user32.dll")] private static extern bool GetLastInputInfo(ref LastInputInfo info); [DllImport("kernel32.dll")] private static extern uint GetTickCount(); } }
Microsoft is conducting an online survey to understand your opinion of the Msdn Web site. If you choose to participate, the online survey will be presented to you when you leave the Msdn Web site.