views:

52

answers:

2

I am currently developing a system that has need for a session time out like subsystem. It is a compact framework 3.5 application, but the principles are very similar to WinForms, so most Winforms solutions will likely work.

Here is the deal, I would like a way to throw an event which lets the MainForm, which is controlling the show, know that the user has not done anything for 20m, so call the logout function and return to the Login form.

This is a "ROUGH" implementation of what I am thinking of. To me, this seems like it smells and is far too simplistic since it assumes ALL activity is routed through a button. I think there must be a way for either the application or the system to know when it is being interacted with.

public partial class MainWindow : Window
{
    private FlowBase CurrentFlow { get; set; }

    private TimerService _service;
    private TimerService CurrentTimerService
    {
        get { return _service ?? (_service = new TimerService()); }
    }

    private TimeSpan _allowedInactivityTime = TimeSpan.FromSeconds(10);
    private TimeSpan _currentInactivityTime = TimeSpan.FromSeconds(0);

    public MainWindow()
    {
        InitializeComponent();
        GoToMainMenu();

        CurrentTimerService.OnTick += (increment) =>
                                        {
                                            if (_currentInactivityTime >= _allowedInactivityTime)
                                            {
                                                ResetInactivityTimer();
                                                GoToMainMenu();
                                            }

                                            _currentInactivityTime = _currentInactivityTime.Add(increment);
                                        };
    }

    private void ResetInactivityTimer()
    {
        _currentInactivityTime = TimeSpan.FromSeconds(0);
    }

    private void btnExit_Click(object sender, RoutedEventArgs e)
    {
        if (ccContent.Content is MainMenu)
            Close();
        else
            CurrentFlow.ExitFlow();
    }

    private void btnNext_Click(object sender, RoutedEventArgs e)
    {
        ResetInactivityTimer();
        ccContent.Content = CurrentFlow.Process(null);
    }

    private void GoToMainMenu()
    {
        var mm = new MainMenu();
        mm.OnFlowSelected += (flow) =>
        {
            CurrentFlow = flow;
            CurrentFlow.OnExit += GoToMainMenu;
            ccContent.Content = flow.Initialize();
        };

        ccContent.Content = mm;
    }
}

Thanks in advance for any help

A: 

Your design is correct. The natural concept of the timeout is implemented by your timer. If you are manually resetting the timer from each one of your calls, try to use any event being fired when the user interacts with the UI. Here's a list.

But, IMO, your solution is fine. That's how most of the session management functions I've seen work.

esegura
A: 

I think this will work http://tinyurl.com/y4lug8

xximjasonxx