我如何知道用户在winform场景中是否活跃?

时间:2014-12-15 04:50:36

标签: winforms logout

要求是,如果用户在过去60秒内没有点击winform应用程序上的任何内容,则应执行自动注销操作。因此,除了重置每个菜单上的计数器,每个按钮等,这是压倒性的,是否有任何更简单的方法来检测用户已停用60秒?

1 个答案:

答案 0 :(得分:1)

我正在使用此代码计算不活动时间。它工作得很好,我希望它也可以帮到你。

public struct LASTINPUTINFO
{
    public uint cbSize;
    public uint dwTime;
}

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

/// <summary>
/// Get inactivity time
/// </summary>
/// <returns>Inactivity time in ms</returns>    
public static int GetIdleTime()
{
    LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
    lastInputInfo.cbSize = (UInt32)   System.Runtime.InteropServices.Marshal.SizeOf(lastInputInfo);

    if (GetLastInputInfo(ref lastInputInfo))
    {
       UInt32 lastInputTick = lastInputInfo.dwTime;
       if (lastInputTick == 0)
          return 0;
       return (Environment.TickCount - (Int32)lastInputInfo.dwTime);
    }
    else
        return 0;
 }


 //In your method :
if (GetIdleTime() >= yourTime)
{
    //logout
}