I have a form that I want to close after 5 seconds if no mouse interaction is done but if any mouse interaction is done I want it to close countdown + 5 seconds
and each interaction would increase it by 5 seconds.
This is what I came up with so far:
int countdown = 5;
System.Timers.Timer timer;
Start timer
timer = new System.Timers.Timer(1000);
timer.AutoReset = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(ProcessTimerEvent);
timer.Start();
The event
private void ProcessTimerEvent(Object obj, System.Timers.ElapsedEventArgs e)
{
--countdown;
if (countdown == 0)
{
timer.Close();
this.Invoke(new Action(() => { this.Close(); }));
}
}
And just for testing I am using the form mouseclick event to increaes the countdown by 5 but will have to change it to a a different event because if you click on a label or any other control on the form it will not increase the timer.
private void NotifierTest_MouseClick(object sender, MouseEventArgs e)
{
countdown += 5;
}
Questions
Am I implementing a countdown where the counter can be increased in a correct way ?
Should I change anything ?
How would you do this if any different from what I have done ?
How should I handle the mouse click capture ?
Using a Low Level Hook ?
Using mouse click position and verify if it was or not on my winform ?
Other option
A different option I am currently thinking is to capture if the mouse is within the form area or not and enable / disable the close countdown if it is not within the area but I am not sure on how to interact with the mouse for this hence the above questions about how would I interact with the mouse.