2

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???

4 Answers 4

2

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();
}
7
  • The drawback is that the user isn't automatically logged out. He has to do something to get logged out. If you mean hooking up mouse and keyboards events and monitoring user activity that way then fine. But if the 'store' is in the DB there is much unnecessary DB access. Better to work with a timer. Apr 29, 2009 at 10:09
  • True. But I think it's rare that we don't check the user and we do something (with|in the name of) the user. If it's really neccessary then Timer can be usefull.
    – boj
    Apr 29, 2009 at 11:40
  • 1
    Another thing of your solution is that a user is ALWAYS logged out after _LOGOUTTIME has passed. You need to store and compare the _LASTACTIONTIME not the login time. But this is probably what you meant. Apr 29, 2009 at 12:14
  • Now I'm being picky but your comment, "Check (ie. in a get)", suggests the code is for a WebApp. Come to think of it a Singleton is unnecessary in a WinApp since all you need is to compare the last action with the current time and there is at any point in time a single user using the WinApp. Well you CAN have several users if you'd like but all you still need is a list (Dictionary as you suggest) of users and lastactiontimes but a Singleton is not needed since there is no possibility of multiple instances being created of that list. I believe you might have misread the question. Or is it me? Apr 29, 2009 at 14:09
  • Ofcourse there can be hundreds or thousands using the WinApp but each user has it's own copy and own list (Dictionary) which does not have to be a Singleton. Apr 29, 2009 at 14:19
2

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.

1
  • You probably don't even need a BackgroundWorker for this -- just use a normal UI-thread timer.
    – itowlson
    Apr 29, 2009 at 9:57
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.