Prevent system win10 to lock the screen but allow to turn off display

Mimoa

Member
Joined
Apr 29, 2020
Messages
24
Programming Experience
1-3
Hi again.
I am trying to write a program that prevents the system lock the screen and you have tzo instert your password again. I cannot make changes to registry so I cannot prolong the time to lock the screen in the power management...
Instead I have made small program that uses SetThreadExecutionState... It works when you have display_needed set - so the display wont turn out and system wont lock the screen. But I can set this feature in power management - just set the display turn-off to 2 hours... done.
BUT. I want the system unlocked and the screen turned off. How could that be done?

Thanks in advance for your advice.
M.

This is actualy winform app. Just with Continuous and System_Required it will still lock the screen.

Dont lock:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Timers;

namespace NespiUI
{
    public partial class NespiUI : Form
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
        [FlagsAttribute]
        public enum EXECUTION_STATE : uint
        {
            ES_AWAYMODE_REQUIRED = 0x00000040,
            ES_CONTINUOUS = 0x80000000,
            ES_DISPLAY_REQUIRED = 0x00000002,
            ES_SYSTEM_REQUIRED = 0x00000001
        }
        public NespiUI()
        {
            InitializeComponent();
            TrayMenuContext();
        }
        private void TrayMenuContext()
        {
            NTIcon.ContextMenuStrip = new ContextMenuStrip();
            NTIcon.ContextMenuStrip.Items.Add("Exit", null, MenuExit_Click);
        }
        private void NESPI_Click(object sender, EventArgs e)
        {
            SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED| EXECUTION_STATE.ES_AWAYMODE_REQUIRED);
            LBL1.Text = "Nespi!";
        }

        private void SPI_Click(object sender, EventArgs e)
        {
            SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
            LBL1.Text = "Můžeš spát";
        }
        private void FrmClose(object sender, FormClosingEventArgs e)
        {
            Hide();
            NTIcon.Visible = true;
            NTIcon.ShowBalloonTip(2000);
            e.Cancel = true;
        }
          private void NTIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
        }
        private void MenuExit_Click(object Sender, EventArgs e)
        {
            SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
            Application.ExitThread();
        }
    }
}
 
Back
Top Bottom