"登录/主窗体/用户操作激活监听/自动注销"模型
- 摘要:本文为完整的"登录/主窗体/用户操作激活监听/自动注销"模型,能够实现在制定时间内用户无操作时自动注销系统,和一致的事件注册框架,可在其上进行扩展应用.
完整的"登录/主窗体/用户操作激活监听/自动注销"模型,能够实现在制定时间内用户无操作时自动注销系统,和一致的事件注册框架,可在其上进行扩展应用.
//登录窗体
public partial class Login : Form
{
//单件模式
protected static Login instance;
public static Login Create()
{
if (instance == null)
instance = new Login();
return instance;
}
private Login()
{
InitializeComponent();
}
private void Login_Load(object sender, EventArgs e)
{
//注册事件处理
CommandHelper.RegisteClickEvent(
this.btnLogin,delegate(object o, EventArgs p)
{
//登录按键事件处理
//用户验证
...
//呈现主窗体
MainForm.Create().Show();
//启用操作激活监听
CommandHelper.PerformActiveEvent = true;
//重置计数器逻辑
CommandHelper.ResetTimeOut();
//隐藏登录窗体
this.Hide();
});
}
}
//主窗体
public partial class MainForm : Form
{
//单件模式
protected static MainForm instance;
public static MainForm Create()
{
if (instance == null)
instance = new MainForm();
return instance;
}
private MainForm()
{
InitializeComponent();
}
/// <summary>
/// 跨线程安全访问模型代码
/// </summary>
private delegate void OnNoActionTimeOutHandler();
/// <summary>
/// 跨线程安全访问模型代码
/// </summary>
private OnNoActionTimeOutHandler onNoActionTimeOutHandler =
new OnNoActionTimeOutHandler(PerformNoActionTimeOut);
/// <summary>
/// 跨线程安全访问模型代码
/// </summary>
private static void PerformNoActionTimeOut()
{
//禁用操作激活监听
CommandHelper.PerformActiveEvent = false;
//显示登录窗体
Login.Create().Show();
//隐藏主窗体
MainForm.Create().Hide();
}
private void MainForm_Load(object sender, EventArgs e)
{
//当用户关闭主窗体的时候退出应用程序
CommandHelper.RegisteFormColsingEvent(this,
delegate(object o, FormClosingEventArgs args)
{
Environment.Exit(0);
});
//注册操作激活监听事件:如果用户不进行任何操作60秒后自动注销
CommandHelper.RegistHandleWhenNoActionTimeOut(60,
delegate(object o, System.Timers.ElapsedEventArgs p)
{
//异步封送模型
BeginInvoke(onNoActionTimeOutHandler);
}
);
}
}
//命令帮助类
public class CommandHelper
{
/// <summary>
/// 是否启用操作激活监听
/// </summary>
protected static bool performActiveEvent = false;
/// <summary>
/// 是否启用操作激活监听
/// </summary>
public static bool PerformActiveEvent
{
get { return performActiveEvent; }
set { performActiveEvent = value; }
}
/// <summary>
/// 时钟对象
/// </summary>
protected static System.Timers.Timer taskInvoker = new System.Timers.Timer();
protected static double interval = 60000d;
/// <summary>
/// 用户没有执行任何操作到达指定的时间间隔(s)后将调用的操作
/// </summary>
/// <param name="second">指定的时间间隔(s)</param>
/// <param name="handler">调用的操作</param>
public static void RegistHandleWhenNoActionTimeOut(int second,
System.Timers.ElapsedEventHandler handler)
{
System.Reflection.EventInfo eventInfo =
taskInvoker.GetType().GetEvent("Elapsed",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.Public);
eventInfo.AddEventHandler(taskInvoker, handler);
interval = second * 1000;
taskInvoker.Interval = interval;
if (performActiveEvent)
taskInvoker.Start();
}
/// <summary>
/// 命令事件触发的计数器清零
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void PerformEventHandler(object sender, EventArgs e)
{
//在此方法中实现对于用户是否操作的状态捕捉;
if (performActiveEvent)
{
ResetTimeOut();
}
}
/// <summary>
/// 重置计数逻辑
/// </summary>
public static void ResetTimeOut()
{
taskInvoker.Stop();
taskInvoker.Interval = interval;
taskInvoker.Start();
}
/// <summary>
/// 注册点击事件
/// </summary>
/// <param name="command">命令对象</param>
/// <param name="handler">响应委托</param>
public static void RegisteClickEvent(object commander,
System.EventHandler handler)
{
System.Reflection.EventInfo eventInfo =
commander.GetType().GetEvent("Click",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.Public);
if (eventInfo != null)
{
eventInfo.AddEventHandler(commander, handler);
eventInfo.AddEventHandler(commander,
new System.EventHandler(PerformEventHandler));
}
}
/// <summary>
/// 注册事件
/// </summary>
/// <param name="commander"></param>
/// <param name="handler"></param>
/// <param name="eventName"></param>
public static void RegisteEvent(object commander,
System.EventHandler handler, string eventName)
{
System.Reflection.EventInfo eventInfo =
commander.GetType().GetEvent(eventName,
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.Public);
if (eventInfo != null)
{
eventInfo.AddEventHandler(commander, handler);
eventInfo.AddEventHandler(commander,
new System.EventHandler(PerformEventHandler));
}
}
/// <summary>
/// 注册关闭事件
/// </summary>
/// <param name="commander"></param>
/// <param name="handler"></param>
public static void RegisteFormColsingEvent(object commander,
FormClosingEventHandler handler)
{
System.Reflection.EventInfo eventInfo =
commander.GetType().GetEvent("FormClosing",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.Public);
if (eventInfo != null)
{
eventInfo.AddEventHandler(commander, handler);
eventInfo.AddEventHandler(commander,
new FormClosingEventHandler(PerformEventHandler));
}
}
//其他事件
}
打印
收藏文章
关闭
该文章已有条评论
我要发表评论
热门文章
推荐文章