如何判断用户是否在操作本程序? 大家好,我需要在程序中判断用户是否有操作,如果持续指定时间内无操作,就退程序。请教大家有什么思路? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 设置一个全局变量,用来判断用户多久没有操作该程序了,每当用户切换到该程序界面的时候,即actived状态的时候把这个全局变量清零,当这个全局变量达到阀值的时候退出程序。 http://www.idcrx.com/zhishiku/_netjishu/2012/0531/17704.html namespace ActivityMonitor{ using System; using System.Runtime.InteropServices; class ActivityMonitor : System.ComponentModel.Component { public void Start() { this.isIdle = false; timer.Elapsed -= Timer_Elapsed; timer.Elapsed += Timer_Elapsed; timer.Interval = this.resolution; timer.Start(); } public void Stop() { timer.Stop(); } public bool IsIdle { get { return this.isIdle; } private set { if (this.isIdle != value) { this.isIdle = value; if (isIdle && OnIdle != null) { OnIdle(this, EventArgs.Empty); } if (!isIdle && OnActive != null) { OnActive(this, EventArgs.Empty); } } } } public int IdleTimeOut { get { return idleTimeOut; } set { idleTimeOut = Math.Max(50, value); } } public int Resolution { get { return resolution; } set { resolution = Math.Max(50, value); } } public event EventHandler OnIdle; public event EventHandler OnActive; private void Timer_Elapsed(object sender, EventArgs e) { LASTINPUTINFO lastInputInfo = new LASTINPUTINFO(); lastInputInfo.size = 8; if (GetLastInputInfo(ref lastInputInfo)) { lastActivity = Math.Max(lastActivity, lastInputInfo.lastTick); int diff = Environment.TickCount - lastActivity; this.IsIdle = diff > this.IdleTimeOut; } } private bool isIdle = false; private int idleTimeOut = 5000; private int resolution = 100; private int lastActivity = Environment.TickCount; private System.Timers.Timer timer = new System.Timers.Timer(); [StructLayout(LayoutKind.Sequential)] private struct LASTINPUTINFO { public int size; public int lastTick; } [DllImport("User32")] private static extern bool GetLastInputInfo(ref LASTINPUTINFO lii); } class UnitTest { static void Main() { ActivityMonitor monitor = new ActivityMonitor(); monitor.IdleTimeOut = 2000; // 2 seconds monitor.OnIdle += delegate { Console.WriteLine("On idle"); }; monitor.OnActive += delegate { Console.WriteLine("On active"); }; monitor.Start(); Console.ReadLine(); } }} [DllImport("User32.dll")]private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);private struct LASTINPUTINFO{ public uint cbSize; public uint dwTime;}LASTINPUTINFO lastInPut = new LASTINPUTINFO();private void timer_Tick(object sender, EventArgs e){ //配置时间内无操作锁定 lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut); GetLastInputInfo(ref lastInPut); long noOperationTime = System.Environment.TickCount - lastInPut.dwTime; if (noOperationTime > 毫秒时间) { //退出程序 }} C#treeview复选框判断是否选中 高分求:单击子窗体按钮怎样在父窗体弹出另一个子窗体????急!!!先谢了。 在非页面里如何得到Request.ServerVariables["HTTPS"] sql语句的问题 想玩一下vs.net 2005,请各位同任出手帮忙提速. 请问怎么用SQL 操作循环呀? 前几天三楼楼长的oa系统有谁调试成功的? 【分享】StackOverflow监控系统Opserver开源 如何建立独立的线程,急! 请大家提供 Visual SourceSafe 6.0c 下载地址 C#窗体 webBrowser控件赋值后无法编辑~~~~~ 请教一个触发按钮事件的问题
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);private struct LASTINPUTINFO
{
public uint cbSize; public uint dwTime;
}
LASTINPUTINFO lastInPut = new LASTINPUTINFO();
private void timer_Tick(object sender, EventArgs e)
{
//配置时间内无操作锁定
lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
GetLastInputInfo(ref lastInPut); long noOperationTime = System.Environment.TickCount - lastInPut.dwTime; if (noOperationTime > 毫秒时间)
{
//退出程序
}}