namespace MyWPF_Samples { using System; using System.Windows.Threading; using System.Windows; public static class UserIdleManager { static DispatcherTimer timer = null; static Window currentActiveWindow = null; public static int LastActivityTime { get; set; } public static int IdleTime { get { return System.Environment.TickCount - LastActivityTime; } } public static void Register(Window window) { if (timer == null) { timer = new DispatcherTimer(DispatcherPriority.Background); timer.Interval = new TimeSpan(5000); timer.Tick += new EventHandler(timer_Tick); } timer.Start(); LastActivityTime = System.Environment.TickCount; currentActiveWindow = window; currentActiveWindow.KeyDown += new System.Windows.Input.KeyEventHandler(window_KeyDown); currentActiveWindow.MouseMove += new System.Windows.Input.MouseEventHandler(window_MouseMove); } static void timer_Tick(object sender, EventArgs e) { if (System.Environment.TickCount - LastActivityTime > 10000) { LastActivityTime = System.Environment.TickCount; MessageBox.Show("User is idle for 1 min"); if (currentActiveWindow != null && timer != null) { currentActiveWindow.Activate(); timer.Start(); } } } public static void UnRegister(Window window) { if (window != null) { window.KeyDown -= new System.Windows.Input.KeyEventHandler(window_KeyDown); window.MouseMove -= new System.Windows.Input.MouseEventHandler(window_MouseMove); } if (timer != null) timer.Stop(); } static void window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { LastActivityTime = e.Timestamp; } static void window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { LastActivityTime = e.Timestamp; } } }
Sample Usage
void Window1_Unloaded(object sender, RoutedEventArgs e) { UserIdleManager.UnRegister(this); } void Window1_Loaded(object sender, RoutedEventArgs e) { UserIdleManager.Register(this); }
No comments:
Post a Comment