Session variable in C# desktop application?
I am developing a C# a stand alone single user desktop application that re开发者_如何学运维quires the user to login to the application. I want to ensure that when there is no activity for 5 minutes or so the application will prompt the user to login again. I have several solution in mind to do this but there do not seem efficient. Previously while doing web programming i was able to do this kind of feature using session variable are there are similiar kind of features in C# that can be used for desktop application.
One way to do this is to set a 5-minute timer that is always running, and logs the user out when it ticks. Then you can have any activity restart the timer from the beginning.
If this is a WinForms app, you can have your top-level forms implement IMessageFilter
. In your PreFilterMessage
function you would restart the timer and return false
for messages that indicate activity (WM_KEYDOWN
, WM_MOUSEMOVE
, etc.) to let everything get processed normally.
You can always add a trigger to auto-logout within a Windows Forms Application. Here is a link with examples with an accepted answer
How can I trigger an auto-logout within a Windows Forms Application?
To monitor user activity, you could create a custom Form-based class from which your application forms will inherit. There you can subscribe to the MouseMove and KeyDown events (setting the KeyPreview property to true), either of which will be raised whenever the user is active. You can then create a System.Threading.Timer, with the due time set to 30 minutes, and postpone it using the Change() method whenever user activity is detected.
No, but session state is just a list of variables to help overcome the stateless nature of web applications. Since desktop applications are not stateless, there's no need. I'd just use a simple timer or something similar and log the user out after 5 minutes of inactivity.
精彩评论