Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / Windows Forms

Windows Services Made Simple

Rate me:
Please Sign up or sign in to vote.
4.62/5 (10 votes)
27 Jun 2007CPOL10 min read 94.6K   6.9K   69  
Describes how to build a Windows Service using the Pegasus Library.
#region Copyright & License
//
// Copyright 2001-2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;

namespace Pegasus.Log4Net.Repository.Hierarchy
{
	/// <summary>
	/// Used internally to accelerate hash table searches.
	/// </summary>
	/// <remarks>
	/// <para>
	/// Internal class used to improve performance of 
	/// string keyed hashtables.
	/// </para>
	/// <para>
	/// The hashcode of the string is cached for reuse.
	/// The string is stored as an interned value.
	/// When comparing two <see cref="LoggerKey"/> objects for equality 
	/// the reference equality of the interned strings is compared.
	/// </para>
	/// </remarks>
	/// <author>Nicko Cadell</author>
	/// <author>Gert Driesen</author>
	internal sealed class LoggerKey
	{
		#region Internal Instance Constructors

		/// <summary>
		/// Construct key with string name
		/// </summary>
		/// <remarks>
		/// <para>
		/// Initializes a new instance of the <see cref="LoggerKey" /> class 
		/// with the specified name.
		/// </para>
		/// <para>
		/// Stores the hashcode of the string and interns
		/// the string key to optimize comparisons.
		/// </para>
		/// <note>
		/// The Compact Framework 1.0 the <see cref="String.Intern"/>
		/// method does not work. On the Compact Framework
		/// the string keys are not interned nor are they
		/// compared by reference.
		/// </note>
		/// </remarks>
		/// <param name="name">The name of the logger.</param>
		internal LoggerKey(string name) 
		{
#if NETCF
			// NETCF: String.Intern causes Native Exception
			m_name = name;
#else
			m_name = string.Intern(name);
#endif
			m_hashCache = name.GetHashCode();
		}

		#endregion Internal Instance Constructors

		#region Override implementation of Object

		/// <summary>
		/// Returns a hash code for the current instance.
		/// </summary>
		/// <returns>A hash code for the current instance.</returns>
		/// <remarks>
		/// <para>
		/// Returns the cached hashcode.
		/// </para>
		/// </remarks>
		override public int GetHashCode() 
		{
			return m_hashCache;
		}

		/// <summary>
		/// Determines whether two <see cref="LoggerKey" /> instances 
		/// are equal.
		/// </summary>
		/// <param name="obj">The <see cref="object" /> to compare with the current <see cref="LoggerKey" />.</param>
		/// <returns>
		/// <c>true</c> if the specified <see cref="object" /> is equal to the current <see cref="LoggerKey" />; otherwise, <c>false</c>.
		/// </returns>
		/// <remarks>
		/// <para>
		/// Compares the references of the interned strings.
		/// </para>
		/// </remarks>
		override public bool Equals(object obj) 
		{
			// Compare reference type of this against argument
			if (((object)this) == obj)
			{
				return true;
			}
			
			LoggerKey objKey = obj as LoggerKey;
			if (objKey != null) 
			{
#if NETCF
				return ( m_name == objKey.m_name );
#else
				// Compare reference types rather than string's overloaded ==
				return ( ((object)m_name) == ((object)objKey.m_name) );
#endif
			}
			return false;
		}

		#endregion

		#region Private Instance Fields

		private readonly string m_name;  
		private readonly int m_hashCache;

		#endregion Private Instance Fields
	}	
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions