代码之家  ›  专栏  ›  技术社区  ›  Sauron

检查应用程序是否在一段时间内处于空闲状态并将其锁定

  •  7
  • Sauron  · 技术社区  · 15 年前

    在我的项目中,我需要一个应用程序锁(与Windows锁相同)。如果应用程序闲置一段时间,应用程序应被锁定,即应用程序的登录窗口将出现。如何在WPF C应用程序中执行此操作?

    3 回复  |  直到 11 年前
        1
  •  13
  •   RRUZ    15 年前

    您可以使用这些功能

    请参阅此代码,您必须向表单中添加一个计时器,并将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();
        }
      }
    }
    
        2
  •  1
  •   Jeremy Thompson    11 年前

    在我看来,公认的答案不如这种方法好:

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

    codeproject文章使用的Windows消息将导致组件 考虑应用程序不空闲

    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>
    
        3
  •  0
  •   Noon Silk    15 年前

    在加载时设置超时,每次发生“活动”操作时(您需要连接到它们),将计时器重置回启动状态。