To monitor user activity, you could create a custom Form-based class from which your application forms will inherit. There you can subscribe to the MouseMove and KeyDown events (setting the KeyPreviewproperty to true), either of which will be raised whenever the user is active. You can then create aSystem.Threading.Timer, with the due time set to 30 minutes, and postpone it using the Change() method whenever user activity is detected.
This is an example implementation below: the ObservedForm is written to be rather general, so that you can more easily see the pattern.
public class ObservedForm : Form
{
public event EventHandler UserActivity;
public ObservedForm()
{
KeyPreview = true;
FormClosed += ObservedForm_FormClosed;
MouseMove += ObservedForm_MouseMove;
KeyDown += ObservedForm_KeyDown;
}
protected virtual void OnUserActivity(EventArgs e)
{
var ua = UserActivity;
if(ua != null)
{
ua(this, e);
}
}
private void ObservedForm_MouseMove(object sender, MouseEventArgs e)
{
OnUserActivity();
}
private void ObservedForm_KeyDown(object sender, KeyEventArgs e)
{
OnUserActivity();
}
private void ObservedForm_FormClosed(object sender, FormClosedEventArgs e)
{
FormClosed -= ObservedForm_FormClosed;
MouseMove -= ObservedForm_MouseMove;
KeyDown -= ObservedForm_KeyDown;
}
}
Now you can subscribe to the UserActivity event, and do the logics you desire, for example:
private System.Threading.Timer timer = new Timer(_TimerTick, null, 1000 * 30 * 60, Timeout.Infinite);
private void _OnUserActivity(object sender, EventArgs e)
{
if(timer != null)
{
timer.Change(1000 * 30 * 60, Timeout.Infinite);
}
}
private void _TimerTick(object state)
{
}
Hope this helps.