Check if an application is idle for a time period and lock it

前端 未结 3 1683
长情又很酷
长情又很酷 2020-12-01 03:27

In my project I need an application lock (same as windows lock). If the application is idle for a time period the application should be locked i.e, the login window for the

相关标签:
3条回答
  • 2020-12-01 03:48

    You can use these functions

    • LockWorkStation
    • GetLastInputInfo

    see this code, you must add a timer to your form, and set this.timer1.Enabled = true;

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace WindowsFormsApplication9
    {
      internal struct LASTINPUTINFO
      {
        public uint cbSize;    
        public uint dwTime;
      }
    
      public partial class Form1 : Form
      {
    
        [DllImport("User32.dll")]
        public static extern bool LockWorkStation();
        [DllImport("User32.dll")]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO Dummy);
        [DllImport("Kernel32.dll")]
        private static extern uint GetLastError();
    
    public static uint GetIdleTime()
    {
      LASTINPUTINFO LastUserAction = new LASTINPUTINFO();
      LastUserAction.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(LastUserAction);
      GetLastInputInfo(ref LastUserAction);
      return ((uint)Environment.TickCount - LastUserAction.dwTime);
    }
    
    public static long GetTickCount()
    {
      return Environment.TickCount;
    }
    
    public static long GetLastInputTime()
    {
      LASTINPUTINFO LastUserAction = new LASTINPUTINFO();
      LastUserAction.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(LastUserAction);
      if (!GetLastInputInfo(ref LastUserAction))
      {
        throw new Exception(GetLastError().ToString());
      }
    
      return LastUserAction.dwTime;
    }
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
          if (GetIdleTime() > 10000)  //10 secs, Time to wait before locking
            LockWorkStation();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
          timer1.Start();
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-01 03:48

    IMO the accepted answer is not as good as this method:

    http://www.codeproject.com/Articles/30345/Application-Idle

    The CodeProject article uses Windows messages that will cause the component to consider the application not idle, eg

    public enum ActivityMessages : int
    {
        /// <summary>
        /// Cursor moved while within the nonclient area.
        /// </summary>
        WM_NCMOUSEMOVE = 0x00A0,
        /// <summary>
        /// Mouse left button pressed while the cursor was within the nonclient area.
        /// </summary>
        WM_NCLBUTTONDOWN = 0x00A1,
        /// <summary>
        /// Mouse left button released while the cursor was within the nonclient area.
        /// </summary>
        WM_NCLBUTTONUP = 0x00A2,
        /// <summary>
    
    0 讨论(0)
  • 2020-12-01 03:59

    Set a timeout on load, and every time an "active" action happens (you'll need to hook up to them), reset the timer back to the start.

    0 讨论(0)
提交回复
热议问题