C#多久是不活动的用户 [英] C# For how long was user inactive

查看:117
本文介绍了C#多久是不活动的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景资料:
我写一个应用程序有几种形式等,用户必须登录才能使用的大多数功能,这工作得很好,直到如今。然而,现在,客户端已请求该用户将一定量的不活动时间之后被退出。的问题是,用户仍然可以被激活的计算机上,只是没有在我的申请,.需要明确的是,我必须注销用户时,他是在我的应用程序不活跃,即使他仍与桌面互动。

Some background: I am writing a application with several forms, etc. Users have to log in in order to use most of the features, and this worked fine until now. However, now, client has requested that the user would be logged out after a certain amount of inactive time. The problem is that the user can still be active on the computer, just not in my application,. To be clear, I have to log the user out when he is inactive in my application, even if he is still interacting with the desktop.

首先,我想这将是相当简单。只记得的最后一个动作的时候,在一个计时器当前时间不断进行比较,并注销用户如果传递的时间比允许的时间越大。不过,我已经意识到,找出最后的操作时间可能不那么简单......

First I thought this would be fairly simple. Just remember the time of the last action, compare it continually in a timer with current time and log out the user if the time passed is greater than the allowed time. However I have realised that finding out the last action time may not be so simple...

当然,我可以复制粘贴类似

Of course I could copy paste something like

Program.LastActionTime = DateTime.Now;

在每一个的OnChange,的OnClick等,活动......然而,不仅这将是一个伟大因为应用程序的大小,工作量......这也将是一个非常不好的做法,我相信这将是至少有一次忘了,使整个事情不可靠的(而出现破裂,该bug几乎不可能重现!)

in every OnChange, OnClick, etc, event ... However not only that this would be a great amount of work because of the size of the application ... It would also be a very bad practice and I'm sure it would be forgot at least once, making the whole thing unreliable (And appear broken, the bug would be almost impossible to reproduce!)

那么,有没有更好的办法?

So, is there a better way?

推荐答案

在您的Program.cs文件,您可以处理Application.Idle事件并重置计时器那里。参见:

In your Program.CS file, you can handle the Application.Idle event and reset your timer there. See:

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.idle.aspx

这篇关于C#多久是不活动的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆