Inactivity and activity ,application idle , user-inactivity auto logout
Asked Answered
G

1

4

After lot of googling and spending 4 hours I guess this is the best way to find user inactive and lock screen.

public MainWindow()
    {
        InitializeComponent();
        var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(10) };
        timer.Tick += delegate
        {
            timer.Stop();
            MessageBox.Show("Logoff trigger");
            timer.Start();
        };
        timer.Start();
        InputManager.Current.PostProcessInput += delegate(object s, ProcessInputEventArgs r)
        {
            if (r.StagingItem.Input is MouseButtonEventArgs || r.StagingItem.Input is KeyEventArgs)
                timer.Interval = TimeSpan.FromSeconds(10);
        };
    }
Gluten answered 26/8, 2014 at 23:3 Comment(3)
And what is your question?Chide
You can ask a code review at codereview.stackexchange.comAssagai
That would be better served by self-answering. Don't put the answer in the question :)Bertina
M
1

If your question is, "Is there a better way to implement a session timeout?" - then I would say, possibly - this is how I do it.

A session timeout can do one of two things. It can work like an arcade game where you have a timeout duration in which to complete your tasks. Or it can monitor for inactivity and close if nothing happens.

Either way, I would wrap this functionality into a session provider - assuming MVVM is the goal, I use provider to refer to a service or data source of some sort.

The provider gets instantiated as a singleton and so exists as one instance throughout the app life cycle. I usually manage this by creating it in app.xaml. I then interact with the provider using a message framework such as MVVM light.

Once created the provider manages a thread which checks a datetime to see if it occurs in the past. If it does it issues a session over event or message. This message is picked up by your application to shut down or whatever.

If you want a finite period or time, you implement a start message which sets the monitored date time to a point in the future according to the time span you want to run. If you want an inactivity log out then you send this message from whatever user interactions you see fit to prevent log out such as input changes or ICommand executes.

I take this a stage further so my provider also issues an 'ending' message a few seconds before the timeout completes so I can display a warning - but hopefully you get the general idea.

If this sounds like what you're after then I will add in some examples, but haven't so far in case this is not what you're asking.

Malnutrition answered 27/8, 2014 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.