Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

c# - How can I trigger an auto-logout within a Windows Forms Application?

I have a Windows App Project to which users can login with their userid and passwords. I want to make it so that when a user logs in, I will get the Login Time, and if the user doesn't use the application for 30 min, the application will send the user to the Login screen again. How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Edit: Adam is absolutely right, I've misunderstood the question, so I deleted my original answer.

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.

This is an example implementation below: the ObservedForm is written to be rather general, so that you can more easily see the pattern.

public class ObservedForm : Form
{
     public event EventHandler UserActivity;

     public ObservedForm()
     {
         KeyPreview = true;

         FormClosed += ObservedForm_FormClosed;
         MouseMove += ObservedForm_MouseMove;
         KeyDown += ObservedForm_KeyDown;
     }

     protected virtual void OnUserActivity(EventArgs e)
     {
         var ua = UserActivity;
         if(ua != null)
         {
              ua(this, e);
         }
     }

     private void ObservedForm_MouseMove(object sender, MouseEventArgs e)
     {
          OnUserActivity();
     }

     private void ObservedForm_KeyDown(object sender, KeyEventArgs e)
     {
          OnUserActivity();
     }

     private void ObservedForm_FormClosed(object sender, FormClosedEventArgs e)
     {
         FormClosed -= ObservedForm_FormClosed;
         MouseMove -= ObservedForm_MouseMove;
         KeyDown -= ObservedForm_KeyDown;
     }
}

Now you can subscribe to the UserActivity event, and do the logics you desire, for example:

private System.Threading.Timer timer = new Timer(_TimerTick, null, 1000 * 30 * 60, Timeout.Infinite);
private void _OnUserActivity(object sender, EventArgs e)
{
     if(timer != null)
     {
         // postpone auto-logout by 30 minutes
         timer.Change(1000 * 30 * 60, Timeout.Infinite);
     }
}

private void _TimerTick(object state)
{
    // the user has been inactive for 30 minutes; log him out
}

Hope this helps.

Edit #2: rephrased some parts of the explanation for clarity, and changed the use of the FormClosing event to FormClosed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...