Click here to Skip to main content
15,891,895 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.5K   6.9K   69  
Describes how to build a Windows Service using the Pegasus Library.
using System;
using System.Threading;

using Pegasus.Diagnostics;
using Pegasus.Log4Net;

namespace Pegasus.Threading
{
	/// <summary>
	/// Summary description for AppThread.
	/// </summary>
	internal class AppThread
	{
		// Local Instance Values
		private ILog m_log = LogManager.GetLogger( typeof( AppThread ) );
		private Thread m_thread;
		private AutoResetEvent m_goEvent = new AutoResetEvent( false );
		private bool m_running = false;
		private bool m_allDone = false;

		private WaitCallback m_callback = null;
		private object m_state = null;
				
		/// <summary>
		/// 
		/// </summary>
		public AppThread()
		{
			m_thread = new Thread( new ThreadStart( ThreadProc ) );
			m_thread.IsBackground = true;
			m_thread.Name = "AppThreadPool " + GetHashCode();
			m_thread.Start();
		}

		/// <summary>
		/// Get weather the thread is running or idle
		/// </summary>
		public bool IsRunning
		{
			get
			{
				return m_running;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public WaitCallback Callback
		{
			get
			{
				return m_callback;
			}

			set
			{
				m_callback = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object StateObject
		{
			get
			{
				return m_state;
			}

			set
			{
				m_state = value;
			}
		}


		/// <summary>
		/// Execute the callback function for this thread
		/// </summary>
		public void Run()
		{
			m_goEvent.Set();
		}

		/// <summary>
		/// Stop processing and shut down the thread
		/// </summary>
		public void Stop()
		{
			m_allDone = true;
			m_goEvent.Set();
		}

		/// <summary>
		/// Blocks the calling thread until a thread terminates.
		/// </summary>
		public void Join()
		{
			m_thread.Join();
		}

		/// <summary>
		/// Raises a ThreadAbortException on the thread, to begin the process of 
		/// terminating the thread. Calling this method usually terminates the thread.
		/// </summary>
		public void Abort()
		{
			m_thread.Abort();
		}

		/// <summary>
		/// 
		/// </summary>
		private void ThreadProc()
		{
			while( !m_allDone )
			{
				m_goEvent.WaitOne();
				m_running = true;

				try
				{
					if( m_callback != null )
					{
						try
						{
							m_callback( m_state );
						}
						catch( Exception e )
						{
							if( UnhandledException != null )
							{
								UnhandledException( this, e ); 
							}

							m_log.Error( "AppThreadPool Unhandled Exception", e );
						}
					}

					m_callback = null;
					m_state = null;

					if( ThreadCompleted != null && !m_allDone )
					{
						ThreadCompleted( this );
					}
				}
				finally
				{
					m_running = false;
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public delegate void ThreadCompletedHandler( AppThread thread );

		/// <summary>
		/// 
		/// </summary>
		public delegate void UnhandledExceptionHandler( AppThread thread, Exception e );

		/// <summary>
		/// 
		/// </summary>
		public event ThreadCompletedHandler ThreadCompleted;

		/// <summary>
		/// 
		/// </summary>
		public event UnhandledExceptionHandler UnhandledException;
	}
}

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