public TimeSpan? GetInactiveTime()
        {
            LASTINPUTINFO info = new LASTINPUTINFO();

            info.cbSize = (uint)Marshal.SizeOf(info);

            if (GetLastInputInfo(ref info))

                return
            TimeSpan.FromMilliseconds(Environment.TickCount - info.dwTime);

            else

                return null;
        }
 static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);