C# 特别是如何实现像会话超时这样的winform会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/847920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:01:17  来源:igfitidea点击:

how to implement winform session like session timeout especially

c#winformssession

提问by black sensei

Hi i've plan to implement a feature like session in window application but not for the purpose to hold use information at first.the main purpose is to log out or at least prompt for login/password after session expires.I can't find information regarding to timeout feature on it online.I would be very gratefull if someone can point me to some ressources or share his experience with us.Thanks for reading this PS: i'm using C#.NET 2.0 with visual studio 2005 merci .

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

采纳答案by Adam Robinson

Your simplest solution would be to use a Timerand set the Durationproperty to the number of milliseconds you'd like in your timeout. Whenever you encounter activity, you can just reset the timer by calling Stopthen immediately calling Start. Place whatever code you'd like in the Timer's Tickevent (assuming this is a System.Windows.Forms.Timer) and you'll be all set.

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

回答by black sensei

I 've come up with some implementation about that session thing i'll like to share especially for those having the same problem.it's not perfect and i'll like smart people like you to give me some guidance to make it acceptable enough. first of all i created a class 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;
        }

    }

}

it's a mdi application so inside the main form i have function startsession

这是一个 mdi 应用程序,所以在主窗体中我有函数 startsession

public frMain()
    {
        InitializeComponent();
        StartSession();
        //SessionChecker();
    }
    public void StartSession()
   {
       try
       {
          usersession.BeginTimer();
       }
       catch (System.Exception ex)
       {
           MessageBox.Show(ex.ToString());
       }

   }

so any time the user click on something, we check the usersession.SessioAlive property like so inside this switch case snippet

所以每当用户点击某个东西时,我们都会在这个 switch case 代码段中像这样检查 usersession.SessioAlive 属性

case "transfer":
                if (!usersession.SessionAlive)
                {
                    new LoginForm().ShowDialog();
                }
                MessageBox.Show("cash back transfert page");
                break;

and inside the loginForm is login is correct we call the usersession.ResetTimer()

在 loginForm 中登录是正确的,我们调用 usersession.ResetTimer()

this.DialogResult = DialogResult.OK;
        usersession.ResetTimer();
        this.Close();

now i really wanted the checking to be run in background that's were i need your advice.here i use forms.timer to create a small job but since forms.timer doesn't have autoreset it doing an endless loop.here is it.it's inside main form

现在我真的希望检查在后台运行,那是我需要你的建议。在这里我使用 forms.timer 创建一个小工作,但由于 forms.timer 没有自动重置它做一个无限循环。这是它。它内部主窗体

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

    }

so what you think i should do so that it doesn't require the user an action before it's prompt for credentials when session expires. thanks for reading this long stuff. :)

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