[英]Detecting Idle status and Logging user out (WinForms)
我目前正在开发一个需要会话超时的系统,例如子系统。 它是一个紧凑的框架3.5应用程序,但是原理与WinForms非常相似,因此大多数Winforms解决方案都可能会起作用。
这是交易,我想要一种引发事件的方法,该事件使控制节目的MainForm知道用户在20m内未做任何事情,因此调用注销函数并返回Login表单。
这是我所想到的“粗糙”实现。 在我看来,这似乎很香,而且太简单了,因为它假定所有活动都通过按钮进行路由。 我认为应用程序或系统必须有一种方法来知道何时与之交互。
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;
}
}
预先感谢您的任何帮助
您的设计是正确的。 超时的自然概念是由计时器实现的。 如果要从每个呼叫中手动重置计时器,请尝试使用当用户与UI交互时触发的任何事件。 这是清单 。
但是,IMO,您的解决方案很好。 这就是我见过的大多数会话管理功能的工作方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.