[英]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.