如何实现会话超时等winform会话
嗨,我打算在窗口应用程序中实现会话等function,但不是为了暂时保留使用信息。主要目的是在会话到期后注销或至少提示登录/密码。我找不到关于在线超时function的信息。如果有人可以指点我的一些资源或与我们分享他的经验,我将非常感激。感谢您阅读本PS:我正在使用C#.NET 2.0和visual studio 2005 merci。
最简单的解决方案是使用Timer
并将Duration
属性设置为您在超时中所需的毫秒数。 无论何时遇到活动,您都可以通过调用Stop
然后立即调用Start
来重置计时器。 在Timer
的Tick
事件中放置你想要的任何代码(假设这是一个System.Windows.Forms.Timer
),你就可以了。
我已经想出了一些关于那个会话的实现,我想特别为那些有相同问题的人分享。这不是完美的,我会像你这样聪明的人给我一些指导,让它足够可接受。 首先,我创建了一个类usersession
using System.Timers; public class usersession { private static bool sessionalive; private static Timer usertimer; public static bool SessionAlive { get { return sessionalive; } set { sessionalive = value; } } public static void BeginTimer() { try { SessionAlive = true; //usertimer.Start(); usertimer = new Timer(int.Parse(ConfigurationManager.AppSettings["sessiontime"].ToString())); usertimer.Enabled = true; usertimer.AutoReset = false; usertimer.Elapsed += new ElapsedEventHandler(DisposeSession); } catch (Exception ex) { return; } } private static void DisposeSession(object source, ElapsedEventArgs e) { try { SessionAlive = false; } catch (System.Exception ex) { return; } } public static void ResetTimer() { try { SessionAlive = true; usertimer.Stop(); usertimer.Start(); } catch (Exception ex) { return; } } }
它是一个mdi应用程序所以在主窗体中我有函数startsession
public frMain() { InitializeComponent(); StartSession(); //SessionChecker(); } public void StartSession() { try { usersession.BeginTimer(); } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); } }
所以每当用户点击某些内容时,我们会检查usersession.SessioAlive属性,就像这样切换案例片段
case "transfer": if (!usersession.SessionAlive) { new LoginForm().ShowDialog(); } MessageBox.Show("cash back transfert page"); break;
在loginForm里面登录是正确的我们调用usersession.ResetTimer()
this.DialogResult = DialogResult.OK; usersession.ResetTimer(); this.Close();
现在我真的希望检查在后台运行,这是我需要你的建议。我使用forms.timer创建一个小工作,但因为forms.timer没有autoreset它做一个无限循环。它是it.it的在主要forms内
private void SessionChecker() { try { check = new Timer(); check.Enabled = true; check.Interval = 1000; check.Tick += new EventHandler(check_Tick); } catch (Exception) { throw; } } void check_Tick(object sender, EventArgs e) { try { if (!usersession.SessionAlive) { new LoginForm().ShowDialog(); check.Stop(); } } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); } }
所以你认为我应该这样做,以便它不需要用户在会话到期时提示输入凭据之前采取行动。 感谢您阅读这些长篇大论。 ?
上述就是C#学习教程:如何实现会话超时等winform会话分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1016263.html