Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C#

How to check for user inactivity with and without platform invokes in C#

Rate me:
Please Sign up or sign in to vote.
4.87/5 (67 votes)
22 Dec 200414 min read 299.1K   7.4K   179  
Within the last month, two fellow programmers asked how to implement a timeout after a certain interval of inactivity. This article features four and a half ways of doing this.
// MonitorBase Class
// Revision 1 (2004-12-18)
// Copyright (C) 2004 Dennis Dietrich
//
// Released unter the BSD License
// http://www.opensource.org/licenses/bsd-license.php

using System;
using System.ComponentModel;
using System.Timers;

namespace UserInactivityMonitoring
{
	/// <summary>
	/// Provides the abstract base class for inactivity
	/// monitors which is independent from how the
	/// events are intercepted
	/// </summary>
	public abstract class MonitorBase : IInactivityMonitor
	{
		#region Private Fields

		private bool disposed        = false;
		private bool enabled         = false;

		private bool monitorMouse    = true;
		private bool monitorKeyboard = true;

		private bool timeElapsed = false;
		private bool reactivated = false;

		private Timer monitorTimer = null;

		#endregion Private Fields

		#region Constructors

		/// <summary>
		/// Creates a new instance of <see cref="MonitorBase"/> which
		/// includes a <see cref="System.Timers.Timer"/> object
		/// </summary>
		protected MonitorBase()
		{
			monitorTimer = new Timer();
			monitorTimer.AutoReset = false;
			monitorTimer.Elapsed += new ElapsedEventHandler(TimerElapsed);
		}

		#endregion Constructors

		#region Dispose Pattern

		/// <summary>
		/// Unregisters all event handlers and disposes internal objects
		/// </summary>
		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		/// <summary>
		/// Actual deconstructor in accordance with the dispose pattern
		/// </summary>
		/// <param name="disposing">
		/// True if managed and unmanaged resources will be freed
		/// (otherwise only unmanaged resources are handled)
		/// </param>
		protected virtual void Dispose(bool disposing)
		{
			if (!disposed)
			{
				disposed = true;
				if (disposing)
				{
					Delegate[] delegateBuffer = null;

					monitorTimer.Elapsed -= new ElapsedEventHandler(TimerElapsed);
					monitorTimer.Dispose();

					delegateBuffer = Elapsed.GetInvocationList();
					foreach (ElapsedEventHandler item in delegateBuffer)
						Elapsed -= item;
					Elapsed = null;

					delegateBuffer = Reactivated.GetInvocationList();
					foreach (EventHandler item in delegateBuffer)
						Reactivated -= item;
					Reactivated = null;
				}
			}
		}

		/// <summary>
		/// Deconstructor method for use by the garbage collector
		/// </summary>
		~MonitorBase()
		{
			Dispose(false);
		}

		#endregion Dispose Pattern

		#region Public Events

		/// <summary>
		/// Occurs when the period of time defined by <see cref="Interval"/>
		/// has passed without any user interaction
		/// </summary>
		public event ElapsedEventHandler Elapsed;

		/// <summary>
		/// Occurs when the user continues to interact with the system after
		/// <see cref="Interval"/> has elapsed
		/// </summary>
		public event EventHandler Reactivated;

		#endregion Public Events

		#region Protected Properties

		/// <summary>
		/// True after <see cref="Elapsed"/> has been raised until
		/// <see cref="Reset"/> is called
		/// </summary>
		protected bool TimeElapsed
		{
			get
			{
				return timeElapsed;
			}
		}

		/// <summary>
		/// True after <see cref="Reactivated"/> has been raised until
		/// <see cref="Reset"/> is called
		/// </summary>
		protected bool ReactivatedRaised
		{
			get
			{
				return reactivated;
			}
		}

		#endregion Protected Properties

		#region Public Properties

		/// <summary>
		/// Period of time without user interaction after which
		/// <see cref="Elapsed"/> is raised
		/// </summary>
		public virtual double Interval
		{
			get
			{
				return monitorTimer.Interval;
			}
			set
			{
				monitorTimer.Interval = value;
			}
		}

		/// <summary>
		/// Specifies if the instances raises events
		/// </summary>
		public virtual bool Enabled
		{
			get
			{
				return enabled;
			}
			set
			{
				monitorTimer.Enabled = enabled = value;
			}
		}

		/// <summary>
		/// Specifies if the instances monitors mouse events
		/// </summary>
		public virtual bool MonitorMouseEvents
		{
			get
			{
				return monitorMouse;
			}
			set
			{
				monitorMouse = value;
			}
		}

		/// <summary>
		/// Specifies if the instances monitors keyboard events
		/// </summary>
		public virtual bool MonitorKeyboardEvents
		{
			get
			{
				return monitorKeyboard;
			}
			set
			{
				monitorKeyboard = value;
			}
		}

		/// <summary>
		/// Object to use for synchronization (the execution of
		/// event handlers will be marshalled to the thread that
		/// owns the synchronization object)
		/// </summary>
		public virtual ISynchronizeInvoke SynchronizingObject
		{
			get
			{
				return monitorTimer.SynchronizingObject;
			}
			set
			{
				monitorTimer.SynchronizingObject = value;
			}
		}

		#endregion Properties

		#region Public Methods

		/// <summary>
		/// Resets the internal timer and status information
		/// </summary>
		public virtual void Reset()
		{
			if (disposed)
				throw new ObjectDisposedException("Object has already been disposed");

			if (enabled)
			{
				monitorTimer.Interval = monitorTimer.Interval;
				timeElapsed = false;
				reactivated = false;
			}
		}

		#endregion Public Methods

		#region Proteced Methods

		/// <summary>
		/// Method to raise the <see cref="Elapsed"/> event
		/// (performs consistency checks before raising <see cref="Elapsed"/>)
		/// </summary>
		/// <param name="e">
		/// <see cref="ElapsedEventArgs"/> object provided by the internal timer object
		/// </param>
		protected void OnElapsed(ElapsedEventArgs e)
		{
			timeElapsed = true;
			if (Elapsed != null && enabled && (monitorKeyboard || monitorMouse))
				Elapsed(this, e);
		}

		/// <summary>
		/// Method to raise the <see cref="Reactivated"/> event (performs
		/// consistency checks before raising <see cref="Reactivated"/>)
		/// </summary>
		/// <param name="e">
		/// <see cref="EventArgs"/> object
		/// </param>
		protected void OnReactivated(EventArgs e)
		{
			reactivated = true;
			if (Reactivated != null && enabled && (monitorKeyboard || monitorMouse))
				Reactivated(this, e);
		}

		#endregion Proteced Methods

		#region Private Methods

		private void TimerElapsed(object sender, ElapsedEventArgs e)
		{
			if (monitorTimer.SynchronizingObject != null)
				if (monitorTimer.SynchronizingObject.InvokeRequired)
				{
					monitorTimer.SynchronizingObject.BeginInvoke(
						new ElapsedEventHandler(TimerElapsed),
						new object[] {sender, e});
					return;
				}			
			OnElapsed(e);
		}

		#endregion Private Methods
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions