Click here to Skip to main content
15,894,343 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 300.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.
// MonitorCreator Class
// Revision 1 (2004-12-22)
// Copyright (C) 2004 Dennis Dietrich
//
// Released unter the BSD License
// http://www.opensource.org/licenses/bsd-license.php

using System.Windows.Forms;

namespace UserInactivityMonitoring
{
	/// <summary>
	/// Static class for creating <see cref="IInactivityMonitor"/> objects
	/// </summary>
	public class MonitorCreator
	{
		private MonitorCreator()
		{
		}

		/// <summary>
		/// Creates a new instance of the default monitor class (global)
		/// </summary>
		/// <returns>Reference to the new  <see cref="IInactivityMonitor"/> object</returns>
		public static IInactivityMonitor CreateInstance()
		{
			return CreateInstance(null, MonitorType.DefaultMonitor);
		}

		/// <summary>
		/// Creates a new instance of a monitor class
		/// </summary>
		/// <param name="type">
		/// Type of the monitor to create
		/// </param>
		/// <returns>Reference to the new  <see cref="IInactivityMonitor"/> object</returns>
		public static IInactivityMonitor CreateInstance(MonitorType type)
		{
			return CreateInstance(null, type);
		}

		/// <summary>
		/// Creates a new instance of a monitor class
		/// </summary>
		/// <param name="target">
		/// <see cref="Control"/> to monitor (may be null if not needed and
		/// is not checked by <see cref="CreateInstance"/> before calling the constructor)
		/// </param>
		/// <param name="type">
		/// Type of the monitor to create
		/// </param>
		/// <returns>Reference to the new  <see cref="IInactivityMonitor"/> object</returns>
		public static IInactivityMonitor CreateInstance(Control target, MonitorType type)
		{
			switch (type)
			{
				case MonitorType.ApplicationMonitor:
					return new ApplicationMonitor();
				case MonitorType.ControlMonitor:
					return new ControlMonitor(target);
				case MonitorType.LastInputMonitor:
					return new LastInputMonitor();
				case MonitorType.ThreadHookMonitor:
					return new HookMonitor(false);
				case MonitorType.GlobalHookMonitor:
				default:
					return new HookMonitor(true);
			}
		}
	}
}

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