Idle Detection in WPF
Asked Answered
F

1

9

I'm new in using WPF so I have no Idea how to detect Idle time and show the main window after 5mins of Idle.

Can anyone help me? Thank you so much.

Fong answered 25/4, 2014 at 7:28 Comment(1)
You need to Maintain a timer for your WPF app .. Which get reset Whwnever any event occurs .. Thats how you can detect Idle time for your ApplicationFever
P
5

You can do;

var timer = new DispatcherTimer (
    TimeSpan.FromMinutes(5),
    DispatcherPriority.ApplicationIdle,// Or DispatcherPriority.SystemIdle
    (s, e) => { mainWindow.Activate(); }, // or something similar
    Application.Current.Dispatcher
);

picked up from here

Properly answered 25/4, 2014 at 7:44 Comment(3)
How would the timer be reset when a user interacts with the application?Swizzle
Good point. maybe recreate the timer as a result of Window.Activated event firing. Then when user brings to focus it resets. Or create the time and just stop and start it on Activated. Maybe use InputManager.Current.PreProcessInput and reset in the handler for thisProperly
A more complete implementation on the same principle is here https://mcmap.net/q/387370/-wpf-inactivity-and-activityProperly

© 2022 - 2024 — McMap. All rights reserved.