Csharp/C#教程:10分钟不活动后关闭C#App分享


10分钟不活动后关闭C#App

我正在研究ac#windows app,我想添加一个function,应用程序将在10分钟不活动后自行关闭。 任何实现代码都将受到欢迎。

您可能需要一些p调用,特别是GetLastInputInfo窗口函数。 它告诉您何时为当前用户检测到最后一个输入(键盘,鼠标)。

上述就是C#学习教程:10分钟不活动后关闭C#App分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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; } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/985547.html

(0)
上一篇 2021年12月21日
下一篇 2021年12月21日

精彩推荐