我正在开发一个c#windows应用程序,我想添加一个功能,应用程序将在10分钟不活动后自行关闭。 任何实施代码都将受到欢迎。
答案 0 :(得分:1)
您可能需要一些p调用,特别是GetLastInputInfo窗口函数。它告诉您何时为当前用户检测到最后一个输入(键盘,鼠标)。
internal class Program {
private static void Main() {
// don't run timer too often, you just need to detect 10-minutes idle, so running every 5 minutes or so is ok
var timer = new Timer(_ => {
var last = new LASTINPUTINFO();
last.cbSize = (uint)LASTINPUTINFO.SizeOf;
last.dwTime = 0u;
if (GetLastInputInfo(ref last)) {
var idleTime = TimeSpan.FromMilliseconds(Environment.TickCount - last.dwTime);
// Console.WriteLine("Idle time is: {0}", idleTime);
if (idleTime > TimeSpan.FromMinutes(10)) {
// shutdown here
}
}
}, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
Console.ReadKey();
timer.Dispose();
}
[DllImport("user32.dll")]
public static extern bool GetLastInputInfo(ref LASTINPUTINFO info);
[StructLayout(LayoutKind.Sequential)]
public struct LASTINPUTINFO {
public static readonly int SizeOf = Marshal.SizeOf(typeof (LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)] public UInt32 cbSize;
[MarshalAs(UnmanagedType.U4)] public UInt32 dwTime;
}
}