2010-07-24 58 views
1

我目前正在开发一个需要像子系统一样会话超时的系统。这是一个紧凑的框架3.5应用程序,但其原理与WinForms非常相似,所以大多数Winforms解决方案都可能工作。检测空闲状态并记录用户输出(WinForms)

这里是处理,我想要一个方法来抛出一个事件,让控制节目的MainForm知道用户没有做任何20米的事情,所以调用注销函数并返回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; 
    } 
} 

预先感谢任何帮助

+0

DispatchTimer不是Winforms,闻起来像WPF。 CF上的许多WinForms类都不可用。像IMessageFilter,你需要的那个。 – 2010-07-24 03:01:12

+0

是的,这也是一个问题。还有另一个可用的定时器类(Not System.Windows.Form.Timer) – xximjasonxx 2010-07-24 23:38:30

回答

0

你的设计是正确的。超时的自然概念由您的计时器实现。如果要手动重置每个调用的计时器,请尝试使用用户与UI交互时触发的任何事件。 Here's a list

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