使用UserControl绘制事件来触发空闲计时器?

时间:2014-01-30 16:52:06

标签: c# timer user-controls

我的任务是在一段时间不活动后触发注销事件。我已经创建了一些似乎在我的应用程序中工作的代码,但我假设我采取了异常方法,因为我没有看到类似的解决方案提出给其他人试图做同样的事情。我的方法依赖于UserControl.Paint事件,有更好的做法吗?我可能不知道的Paint事件有限制吗?

public partial class MyControl : UserControl
{
    private Timer _idleTimer;
    private SubControl _sc;

    public MyControl()
    {
        InitializeComponent();

        // Create the auto logout timer
        _idleTimer = new Timer();
        _idleTimer.Interval = 300000;
        _idleTimer.Tick += btnLogout_Click;
        _idleTimer.Enabled = true;

        // Create the subcontrol.
        _sc = new SubControl();
        _sc.Paint += (o, i) => _idleTimer.Reset(); // Extension method that call Stop and Start
        this.tabPage1.Controls.Add(_sc);
    }
}

谢谢 - 德里克

1 个答案:

答案 0 :(得分:2)

请尝试使用Application.Idle事件:

Application.Idle += (o, i) => { _idleTimer.Reset(); };

paint事件并不总是触发,所以它不可靠作为你的不活动来源。

相关问题