If you want a more dynamic approach you could subscribe to all of the events in your Form
because ultimately if a user is idle no events should ever be raised.
private void HookEvents()
{
foreach (EventInfo e in GetType().GetEvents())
{
MethodInfo method = GetType().GetMethod("HandleEvent", BindingFlags.NonPublic | BindingFlags.Instance);
Delegate provider = Delegate.CreateDelegate(e.EventHandlerType, this, method);
e.AddEventHandler(this, provider);
}
}
private void HandleEvent(object sender, EventArgs eventArgs)
{
lastInteraction = DateTime.Now;
}
You could declare a global variable private DateTime lastInteraction = DateTime.Now;
and assign to it from the event handler. You could then write a simple property to determine how many seconds have elapsed since the last user interaction.
private TimeSpan LastInteraction
{
get { return DateTime.Now - lastInteraction; }
}
And then poll the property with a Timer
as described in the original solution.
private void timer1_Tick(object sender, EventArgs e)
{
if (LastInteraction.TotalSeconds > 90)
{
MessageBox.Show("Idle!", "Come Back! I need You!");
}
}