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

发布于 2024-09-11 08:49:10 字数 2157 浏览 0 评论 0原文

我目前正在开发一个需要会话超时的系统,例如子系统。它是一个紧凑的框架 3.5 应用程序,但原理与 WinForms 非常相似,因此大多数 Winforms 解决方案都可以工作。

事情是这样的,我想要一种方法来抛出一个事件,让控制显示的 MainForm 知道用户在 20m 内没有执行任何操作,因此调用注销函数并返回到登录表单。

这是我的想法的“粗略”实现。对我来说,这似乎有点奇怪,而且过于简单化,因为它假设所有活动都通过按钮进行路由。我认为必须有一种方法可以让应用程序或系统知道何时与之交互。

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;
    }
}

预先感谢您的任何帮助

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-09-18 08:49:10

你的设计是正确的。超时的自然概念是由计时器实现的。如果您在每次调用中手动重置计时器,请尝试使用用户与 UI 交互时触发的任何事件。 这里有一个列表

但是,IMO,你的解决方案很好。这就是我见过的大多数会话管理功能的工作原理。

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. Here's a list.

But, IMO, your solution is fine. That's how most of the session management functions I've seen work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文