Winform学习(七)--长时间不操作则退出登陆
1、定义无操作时间
static int iOperCount = 0;//无操作秒数 static int logoutSecond = 10;//退出倒计时
2、动作后无操作描述归零
//用于记录用户无操作时间 internal class MyMessager : IMessageFilter { public bool PreFilterMessage(ref Message m) { //如果检测到有鼠标或则键盘的消息,则使计数为0 if (m.Msg == 0x0202) { iOperCount = 0; } return false; } }
3、添加定时任务
//定时任务,检查无操作时间 private void timer1_Tick(object sender, EventArgs e) { if (!User.currentUser.LoginStatus)//未登录 { return; } iOperCount++; if (iOperCount >= iOperLogout) { if (iOperCount == iOperLogout) { logoutSecond = 10; timer2.Enabled = true; } } else { timer2.Enabled = false; labelTip1.Visible = false; } }
4、显示倒计时退出提示
private void timer2_Tick(object sender, EventArgs e) { logoutSecond = logoutSecond - 1; string tip1 = "用户长时间未操作,系统将于" + logoutSecond.ToString() + "秒后退出登陆……"; labelTip1.Text = tip1; labelTip1.Visible = true; if (logoutSecond == 0) { logout(); iOperCount = 0; } }