GetlastInput and Tickcount

I have the following code piece for a console application that I would like to lock my computer if the idle time exceed 5 seconds.

Problem: there is nothing happens after 5 seconds at all

    public static uint GetIdleTime()
    {
        LASTINPUTINFO lastInPut = new LASTINPUTINFO();
        lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
        GetLastInputInfo(ref lastInPut);

        return ((uint)Environment.TickCount - lastInPut.dwTime);
    }

    public static long GetTickCount()
    {
        return Environment.TickCount;
    }

    public static long GetLastInputTime()
    {
        LASTINPUTINFO lastInPut = new LASTINPUTINFO();
        lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
        if (!GetLastInputInfo(ref lastInPut))
        {
            throw new Exception(GetLastError().ToString());
        }
        return lastInPut.dwTime;
    }


private static Timer timer;
    private static uint idle = 0;
    static void Main(string[] args)
    {
        timer = new Timer();
        timer.Enabled = true;
        timer.Interval = 1000;
        timer.Start();

        idle += GetIdleTime();            
        System.Threading.Thread.Sleep(6000);
        if (idle > 5000)
        {
            LockWorkStation();
        }
    }

Answers


There are one or two strange things about your code. The timer isn't doing much, you are accumulating idle time, and you're testing the idle time 6 seconds after you've calculated it without recalculating it.

Try This:

    public static uint GetIdleTime()
    {
        LASTINPUTINFO lastInPut = new LASTINPUTINFO();
        lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
        GetLastInputInfo(ref lastInPut);

        return ((uint)Environment.TickCount - lastInPut.dwTime);
    }

    public static long GetTickCount()
    {
        return Environment.TickCount;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct LASTINPUTINFO
    {
        public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));

        [MarshalAs(UnmanagedType.U4)]
        public UInt32 cbSize;
        [MarshalAs(UnmanagedType.U4)]
        public UInt32 dwTime;
    }

    [DllImport("user32.dll")]
    static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool LockWorkStation();

    static int GetLastInputTime()
    {
        int idleTime = 0;
        LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
        lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
        lastInputInfo.dwTime = 0;

        int envTicks = Environment.TickCount;

        if (GetLastInputInfo(ref lastInputInfo))
        {
            int lastInputTick = (int)lastInputInfo.dwTime;

            idleTime = envTicks - lastInputTick;
        }

        return ((idleTime > 0) ? (idleTime / 1000) : 0);
    }


    private static uint idle = 0;
    static void Main(string[] args)
    {
        while (!Console.KeyAvailable)
        {
            System.Threading.Thread.Sleep(500);
            idle = GetIdleTime();
            Console.WriteLine(idle);
            if (idle > 5000)
            {
                LockWorkStation();
            }
        }
    }

Need Your Help

PHP submit button in echo that can do a little javascripting

javascript php html button

I currently have a script that outputs certain values in a db and it's working as intended. But now, I am trying to add a column with buttons. Very every row that the db spits out, I need a button ...

Assigning users in TFS if two fields have specific values

tfs tfs2010 tfs-process-template

Updated question with additional info as solution was not applicable.