C# 检查应用程序是否空闲一段时间并锁定它
在我的项目中,我需要一个应用程序锁(与windows锁相同)。如果应用程序空闲一段时间,应用程序应被锁定,即,应用程序的登录窗口将出现。如何在WPF C#应用程序中实现这一点 设置加载超时,每次“活动”操作发生时(您需要连接到它们),将计时器重置回开始状态。您可以使用这些功能C# 检查应用程序是否空闲一段时间并锁定它,c#,wpf,locking,C#,Wpf,Locking,在我的项目中,我需要一个应用程序锁(与windows锁相同)。如果应用程序空闲一段时间,应用程序应被锁定,即,应用程序的登录窗口将出现。如何在WPF C#应用程序中实现这一点 设置加载超时,每次“活动”操作发生时(您需要连接到它们),将计时器重置回开始状态。您可以使用这些功能 看到这段代码,您必须在表单中添加一个计时器,并设置为this.timer1.Enabled=true using System; using System.Collections.Generic; using Sy
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();
}
}
}
IMO公认的答案不如此方法:
CODESTATION文章使用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>
公共枚举活动消息:int
{
///
///光标在非客户端区域内移动。
///
WMncmousemove=0x00A0,
///
///光标在非客户端区域内时按下鼠标左键。
///
WM_nClubuttonDown=0x00A1,
///
///鼠标左键在光标位于非客户端区域内时释放。
///
WM_nup=0x00A2,
///
是否有可用的示例应用程序?您知道如何实现MVC应用程序吗?这会检测整个系统何时空闲,而不是特定应用程序何时空闲。