【问题标题】:how to implement winform session like session timeout especially如何实现诸如会话超时之类的winform会话,尤其是
【发布时间】:2012-09-25 10:24:20
【问题描述】:

您好,我计划在窗口应用程序中实现类似会话的功能,但最初不是为了保存使用信息。主要目的是在会话过期后注销或至少提示输入登录名/密码。我可以'在网上找不到有关超时功能的信息。如果有人可以向我指出一些资源或与我们分享他的经验,我将非常感激。感谢您阅读本文 PS:我在 Visual Studio 2005 merci 中使用 C#.NET 2.0。

【问题讨论】:

    标签: c# winforms session


    【解决方案1】:

    您最简单的解决方案是使用Timer 并将Duration 属性设置为您希望超时的毫秒数。每当您遇到活动时,您可以通过调用Stop 然后立即调用Start 来重置计时器。在TimerTick 事件(假设这是System.Windows.Forms.Timer)中放置您想要的任何代码,然后一切就绪。

    【讨论】:

    • 感谢您提请我注意计时器类。我还记录用户活动。每个操作都包含在特定方法中,因此每个用户界面组件上的点击事件(对点击事件执行操作)我将方法名称记录为数据库中的活动。我可以使用相同的模型来重置计时器吗?感谢您的快速回复感谢它
    • 当然可以。我不知道它是否适合您的情况(可能是,也可能不是),但它肯定会起作用。例如,请确保您不会使用此函数来记录任何不是由用户输入驱动的消息,因为这会错误地重置您的超时。
    • 谢谢老兄真的很感激。我现在会实现这个,看看如何改进并将其扩展到鼠标事件和键盘事件。谢谢
    【解决方案2】:

    我想出了一些关于该会话的实现,我想分享一下,特别是对于那些有同样问题的人。它并不完美,我希望像你这样的聪明人给我一些指导,使其可以接受足够的。 首先我创建了一个类 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 属性,就像在这个 switch case sn-p 中一样

    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 没有自动重置,它会执行无限循环。这里是it.it 在主窗体内

     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());
            }
    
        }
    

    所以您认为我应该这样做,以便在会话到期时提示输入凭据之前不需要用户执行操作。 感谢您阅读这么长的内容。 :)

    【讨论】:

      猜你喜欢
      • 2014-06-28
      • 1970-01-01
      • 2017-04-21
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 2018-09-23
      相关资源
      最近更新 更多