简体   繁体   English

检测空闲状态并注销用户(WinForms)

[英]Detecting Idle status and Logging user out (WinForms)

I am currently developing a system that has need for a session time out like subsystem. 我目前正在开发一个需要会话超时的系统,例如子系统。 It is a compact framework 3.5 application, but the principles are very similar to WinForms, so most Winforms solutions will likely work. 它是一个紧凑的框架3.5应用程序,但是原理与WinForms非常相似,因此大多数Winforms解决方案都可能会起作用。

Here is the deal, I would like a way to throw an event which lets the MainForm, which is controlling the show, know that the user has not done anything for 20m, so call the logout function and return to the Login form. 这是交易,我想要一种引发事件的方法,该事件使控制节目的MainForm知道用户在20m内未做任何事情,因此调用注销函数并返回Login表单。

This is a "ROUGH" implementation of what I am thinking of. 这是我所想到的“粗糙”实现。 To me, this seems like it smells and is far too simplistic since it assumes ALL activity is routed through a button. 在我看来,这似乎很香,而且太简单了,因为它假定所有活动都通过按钮进行路由。 I think there must be a way for either the application or the system to know when it is being interacted with. 我认为应用程序或系统必须有一种方法来知道何时与之交互。

public partial class MainWindow : Window
{
    private FlowBase CurrentFlow { get; set; }

    private TimerService _service;
    private TimerService CurrentTimerService
    {
        get { return _service ?? (_service = new TimerService()); }
    }

    private TimeSpan _allowedInactivityTime = TimeSpan.FromSeconds(10);
    private TimeSpan _currentInactivityTime = TimeSpan.FromSeconds(0);

    public MainWindow()
    {
        InitializeComponent();
        GoToMainMenu();

        CurrentTimerService.OnTick += (increment) =>
                                        {
                                            if (_currentInactivityTime >= _allowedInactivityTime)
                                            {
                                                ResetInactivityTimer();
                                                GoToMainMenu();
                                            }

                                            _currentInactivityTime = _currentInactivityTime.Add(increment);
                                        };
    }

    private void ResetInactivityTimer()
    {
        _currentInactivityTime = TimeSpan.FromSeconds(0);
    }

    private void btnExit_Click(object sender, RoutedEventArgs e)
    {
        if (ccContent.Content is MainMenu)
            Close();
        else
            CurrentFlow.ExitFlow();
    }

    private void btnNext_Click(object sender, RoutedEventArgs e)
    {
        ResetInactivityTimer();
        ccContent.Content = CurrentFlow.Process(null);
    }

    private void GoToMainMenu()
    {
        var mm = new MainMenu();
        mm.OnFlowSelected += (flow) =>
        {
            CurrentFlow = flow;
            CurrentFlow.OnExit += GoToMainMenu;
            ccContent.Content = flow.Initialize();
        };

        ccContent.Content = mm;
    }
}

Thanks in advance for any help 预先感谢您的任何帮助

Your design is correct. 您的设计是正确的。 The natural concept of the timeout is implemented by your timer. 超时的自然概念是由计时器实现的。 If you are manually resetting the timer from each one of your calls, try to use any event being fired when the user interacts with the UI. 如果要从每个呼叫中​​手动重置计时器,请尝试使用当用户与UI交互时触发的任何事件。 Here's a list . 这是清单

But, IMO, your solution is fine. 但是,IMO,您的解决方案很好。 That's how most of the session management functions I've seen work. 这就是我见过的大多数会话管理功能的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM