在特定时间之后退出窗口应用程序 [英] Log out window application after perticular time

查看:77
本文介绍了在特定时间之后退出窗口应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个窗口应用程序,我希望应用程序在用户不在应用程序中执行任何事件时获取Logout(即使没有鼠标移动或在应用程序上没有任何按键事件),然后应用程序获取注销

解决方案

要监视用户活动,您可以创建一个自定义的基于表单的类,您的应用程序表单将从该类继承。在那里,您可以订阅MouseMove和KeyDown事件(将KeyPreview属性设置为true),无论何时用户处于活动状态,都会引发其中任何一个。然后,您可以创建一个System.Threading.Timer,将截止时间设置为30分钟,并在检测到用户活动时使用Change()方法将其推迟。

这是下面的示例实现: ObservedForm写得相当通用,这样你就可以更容易地看到模式。



  public   class  ObservedForm:表单
{
public < span class =code-keyword> event EventHandler UserActivity;

public ObservedForm()
{
KeyPreview = true ;

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

受保护 虚拟 void OnUserActivity(EventArgs e)
{
var ua = UserActivity;
if (ua!= null
{
ua( ,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;
}
}

现在您可以订阅UserActivity 事件 您需要的逻辑, 示例:
private 系统。 Threading.Timer timer = new 计时器(_TimerTick, null 1000 * 30 * 60 ,Timeout.Infinite);
private void _OnUserActivity( object sender,EventArgs e)
{
if (timer!= null
{
// 推迟自动退出30分钟
timer.Change( 1000 * 30 * 60 ,Timeout.Infinite);
}
}

私有 void _TimerTick( object state)
{
// 用户已停用30分钟;让他退出
}





希望这会有所帮助。


另请参阅此替代解决方案。



http://stackoverflow.com/questions/13210142/how-do-i-determine-idle-time-in-my-c-sharp-application [ ^ ]

I have created window application in that i want application gets Logout when user not do any event with application (even no mouse move or no any key press event on application) in perticular time span then application gets logout

解决方案

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 KeyPreviewproperty to true), either of which will be raised whenever the user is active. You can then create aSystem.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.


Also look at this alternative solution.

http://stackoverflow.com/questions/13210142/how-do-i-determine-idle-time-in-my-c-sharp-application[^]


这篇关于在特定时间之后退出窗口应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆