Hello, I've got a code (modified but not written by me) which uses a timer to find the idle time, and I added the code to make the computer go to sleep mode if the timer matches the time value set.
However, when I resume the computer, it goes back again to the sleep mode almost instantly unless I do a quick shutdown when it is resuming by pressing the power button and then switch on again thus showing that somewhere it is still keeping the values to be compared to start the sleep action. I would like to instead restart the timer counts before it goes for sleep so that the values to be compared to do not match and allows me to resume normally. Anyone can help me? Am still learning C# syntax and other stuff, so please be patient with me ;)

private void tmrIdle_Tick(object sender, EventArgs e)
            // Get the system uptime
            int systemUptime = Environment.TickCount;
            // The tick at which the last input was recorded
            int LastInputTicks = 0;
            // The number of ticks that passed since last input
            int IdleTicks = 0;

            // Set the struct
            LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
            LastInputInfo.cbSize = (uint)Marshal.SizeOf(LastInputInfo);
            LastInputInfo.dwTime = 0;

            // If we have a value from the function
            if (GetLastInputInfo(ref LastInputInfo))
                // Get the number of ticks at the point when the last activity was seen
                LastInputTicks = (int)LastInputInfo.dwTime;
                // Number of idle ticks = system uptime ticks - number of ticks at last input
                IdleTicks = systemUptime - LastInputTicks;

            // Set the labels; divide by 1000 to transform the milliseconds to seconds
            lblSystemUptime.Text = Convert.ToString(systemUptime / 1000) + " seconds";
            lblIdleTime.Text = Convert.ToString(IdleTicks / 1000); //+ " seconds";
            lblLastInput.Text = "At second " + Convert.ToString(LastInputTicks / 1000);

            int itime = Convert.ToInt32(lblIdleTime.Text);

            if (itime > trackBar1.Value)
                Application.SetSuspendState(PowerState.Suspend, false, false);


Edited 4 Years Ago by Ritesh_4

You will need to have a static value that will represent the last systemuptime since you suspended the computer, not from the very first start.

Hope this helps

Edited 4 Years Ago by lolafuertes

This question has already been answered. Start a new discussion instead.