Question

I am working on a windows application. I need to simulate Session (that we have in a web app) in the win app where if the user is inactive for certain period, he gets logged off. The user login status is maintained in the database.

Any innovative ideas???

Was it helpful?

Solution

You do not need Session (or CallContext, or anything else), just a Singleton "user store" with one restriction:

After the user logged in or showed some activity, you have to save the date/time of that. Next time, when the user wants do something, just compare (lastactivity + logouttime) to the actual date/time.

Outline of the process could be:

              [User login]
                   |
                   !
 [User 'store' saves user date + login time]
 [This is a singleton                      ]

                  ...

[Next time user wants to do something. The   ]
[program asks user data from the user 'store']
                   |
                   !
[If the actual time is greater than user     ]
[lastactivity + LOGOUTTIME, user cannot do it]
[If not, then update last activity           ]

UserStore can be implemented as a Dictionary and used as:

// Log in
Singleton.UserStore.Add("John", new UserData( yourUserObject, DateTime.Now));

...

// Check (ie. in a property-get)
var userData = Singleton.UserStore["John"];
if (userData.LastActivityDate + _LOGOUTIME > DateTime.Now()) 
{
   throw UserAutomaticallyLoggedOut();
}
else
{
   userData.LastActivityDate = DateTime.Now();
}

OTHER TIPS

What comes to mind is to use a BackgroundWorker to log off the user when a timer reaches zero. This timer is reset on every action the user makes. Or it could even be hooked to mouse or keyboard events so that when the user doesn't move the mouse or isn't using the keyboard the timer counts down until it reaches the log off time.

You could create a class in your application that has some sort of timeout that is reset every time the user interacts with the software. If the timeout is reached, the user is logged out.

The tricky thing here is to detect user interaction. In ASP.NET is pretty easy, you essentially need to check only for page requests. In a windows forms application you will need to montor input on a much more detailed level (any key press in any text box will be a user interaction for instance). I guess the KeyPreview property together with a KeyPress event listener on the form might be a good way forward.

Well, first, what do you mean by inactive? On the Web you are trying to simulate state where there is none. However, in a client app, you get all sorts events even MouseMove. Onde idea is that you could create UserControls out of the standard input controls like TextBox, Button, and so forth and have them update some sort of timer object when events are invoked. The other is to forego the UserControl stuff and just update the timer in each event handler you create. Probably it would be simpler to just update the timer based on the MouseMove or KeyDown events on the Form itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top