如果没有移动,5秒后(空闲)在winform app中关闭会话

Ari*_*MAZ 0 c# session winforms

我正在编写ac#Winform应用程序,如果计算机空闲5秒钟,我需要关闭会话.该应用程序就像一个餐馆应用程序,当服务员离开他的会话时,我将在5秒后关闭它.

我发现了一些代码,但我不知道如何使用它以及如何触发它

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);   

internal struct LASTINPUTINFO
{
    public uint cbSize;

    public uint dwTime;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我吗?

Ari*_*ria 6

1-添加Timer到您的表单.

2-将其间隔设置为1000.

3 - 将此方法添加到Form类.

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);
}
Run Code Online (Sandbox Code Playgroud)

4- Form启动计时器:

timer1.Start(); 5-计时器嘀嗒事件检查form_load

private void timer1_Tick(object sender, EventArgs e)
{
    if (GetIdleTime() > 5000)  
       Application.Exit();//For Example
}
Run Code Online (Sandbox Code Playgroud)