如果没有移动,则在 5 秒后(空闲)关闭 winform 应用程序中的会话 [英] close session in winform app after 5 seconds(idle) if no movement
问题描述
我正在编写一个 c# Winform 应用程序,如果计算机空闲 5 秒,我需要关闭会话.该应用程序就像一个餐厅应用程序,当服务员打开他的会话时,我会在 5 秒后关闭它.
I am writing a c# Winform application and I need to close session if the computer is idle for 5 seconds. The application is like a restaurant application, when the waiter leaves his session open, I will close it after 5 seconds.
我找到了一些代码,但我不知道如何使用它以及如何触发它
I found some code but I dont know how to use it and how to trigger it
using System.Runtime.InteropServices;
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
有人可以帮我吗?
推荐答案
请按照以下步骤操作:
1- 将 Timer
添加到您的 Form
.
1- Add a Timer
to your Form
.
2- 将其间隔属性设置为 1000(在 form_load
或在属性窗口的设计模式中设置).
2- Set its interval property to 1000 (set it in form_load
or in design mode from Properties window).
3-将此方法添加到您的 Form
类中.
3-Add this method to your Form
class.
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);
}
4-在Form_Load
中启动定时器:
timer1.Start();
5- 在定时器 tick
事件检查 GetIdleTime()
,例如如果它大于 5000
表示应用程序空闲 5 秒之前.
5- in timer tick
event check GetIdleTime()
, for example if it is greater than 5000
means application was idled since 5 seconds ago.
private void timer1_Tick(object sender, EventArgs e)
{
if (GetIdleTime() > 5000)
Application.Exit();//For Example
}
这篇关于如果没有移动,则在 5 秒后(空闲)关闭 winform 应用程序中的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!