views:

968

answers:

2

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 .

+3  A: 

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

Adam Robinson
Thanks for drawing my attention to the timer class.I also log user activity.Every action is contained in a specific method so on click event on every user interface component(that does an action on click event) i log the method name as activity in the database.Can i use the same model to reset the timer?Thanks for the fast reply appreciate it
black sensei
Sure you could. I don't know whether or not it's appropriate in your case (it could be, or it could not be), but it would certainly work. Just be certain, for example, that you wouldn't use this function to log any messages that AREN'T driven by user input, as that would incorrectly reset your timeout.
Adam Robinson
thanks dude really really appreciate it.i would implement this one for now and see how to improve and extend it to mouse event and keyboard event.thanks
black sensei
+1  A: 

hello good people.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

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

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

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

 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. :)

black sensei