/// <summary>
		/// Adds a thread to the list
		/// </summary>
		/// <param name="thread"></param>
		public virtual void Add(BackgroundThread thread)
		{
			if (this.Contains(thread))
				throw new BackgroundThreadAlreadyExistsException(thread);

			base.InnerList.Add(thread);
		}
		/// <summary>
		/// Determines if the list contains the thread
		/// </summary>
		/// <param name="thread"></param>
		/// <returns></returns>
		public virtual bool Contains(BackgroundThread thread)
		{
			if (thread == null)
				throw new ArgumentNullException("thread");

			foreach(BackgroundThread t in base.InnerList)
				if (object.Equals(t, thread))
					return true;
			return false;
		}
		protected virtual void Dispose(bool disposing)
		{
			if (!_disposed)
			{
				if (disposing)
				{
					// dispose of the processing thread
					_processingThread.Dispose();
					_processingThread = null;

					// dispose of all of the thread pool threads
					this.DestroyThreads(false /* all threads */);
				}
				_disposed = true;
			}
		}
예제 #4
0
		/// <summary>
		/// Asyncronously begins a background thread which pings the address X number of times
		/// </summary>
		public void BeginPinging(string address, int timesToPing)
		{	           
			// if the thread is null reset it
			if (_thread == null)
			{
				// each instance of the engine will use a background thread to perform it's work
				_thread = new BackgroundThread();
				_thread.Run += new BackgroundThreadStartEventHandler(OnThreadRun);
				_thread.Finished += new BackgroundThreadEventHandler(OnThreadFinished);
				_thread.AllowThreadAbortException = true;
			}

			// if the thread is not running
			if (!_thread.IsRunning)
				// start it up
				_thread.Start(true, new object[] {address, timesToPing});			
		}
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the BackgroundThreadEventArgs class
 /// </summary>
 /// <param name="thread">The background thread about which this event is centered</param>
 public BackgroundThreadEventArgs(BackgroundThread thread) : base()
 {
     _thread = thread;
 }
		public BackgroundThreadAlreadyExistsException(BackgroundThread thread) : base("The background thread already exists in the list.")
		{
			_thread = thread;
		}
		/// <summary>
		/// Stops the background thread that is receiving messages for the connection
		/// </summary>
		/// <param name="disconnect">A flag that indicates whether the connection should be closed</param>
		public virtual void EndReceiving(bool disconnect)
		{
			_disconnectOnStop = disconnect;

			// signal a stop, we might get lucky and ease the thread out
			if (_stopEvent != null)
				_stopEvent.Set();

			// and finally kill the receiving thread
			if (_thread != null) 
			{
				_thread.Dispose();
				_thread = null;				
			}
			_stopEvent = null;
		}
		/// <summary>
		/// Starts a background thread that will receive messages for the connection
		/// </summary>
		public virtual void BeginReceiving()
		{
			if (this.IsReceiving)
				return;

			_disconnectOnStop = false;
			_stopEvent = new ManualResetEvent(false);
			_thread = new BackgroundThread();
//			_thread.IsBackground = false;
			_thread.AllowThreadAbortException = true;
			_thread.Run += new BackgroundThreadStartEventHandler(OnReadFromSocket);
			_thread.Start(true /* background thread */, new object[] {});
		}
		/// <summary>
		/// Removes a thread from the list
		/// </summary>
		/// <param name="thread"></param>
		public virtual void Remove(BackgroundThread thread)
		{
			if (this.Contains(thread))
				base.InnerList.Remove(thread);
		}
		/// <summary>
		/// Starts a background thread that will monitor the job queue and spawn background threads to process the jobs in the queue
		/// </summary>
		protected virtual void StartProcessingJobs()
		{
			_processingThread = new BackgroundThread();
			_processingThread.AllowThreadAbortException = true;
			_processingThread.Run += new BackgroundThreadStartEventHandler(OnProcessJobs);
			_processingThread.Start(true, new object[] {_maxThreads});			
		}
		/// <summary>
		/// Destroys the specified thread
		/// </summary>
		/// <param name="thread"></param>
		/// <param name="force"></param>
		protected virtual void DestroyThread(BackgroundThread thread, bool force)
		{
			try
			{
				if (thread != null)
					if (thread.IsFinished || force)
						thread.Dispose();
			}
			catch(ThreadAbortException)
			{
			}
			catch(Exception ex)
			{
				Debug.WriteLine(ex);
			}
		}  
예제 #12
0
		/// <summary>
		/// Stops the server (No incoming connections will be accepted while the server is stopped)
		/// </summary>
		/// <returns></returns>
		public bool Stop(bool endCurrentSessions)
		{
			try
			{                       
				if (endCurrentSessions)
					this.EndCurrentSessions();

				if (_thread != null)
				{
					_thread.Dispose();
					_thread = null;
				}

				if (_listeningSocket != null)
				{
					try 
					{
						// shutdown and close the socket
						//             _socket.Shutdown(SocketShutdown.Both);
						_listeningSocket.Close();
					}
					catch(SocketException ex) 
					{
						Debug.WriteLineIf(_verbose, string.Format("An exception was encountered while attempting to shutdown & close the server's listening socket.\n\t{0}", ex.ToString()), MY_TRACE_CATEGORY);
					}
					_listeningSocket = null;
				}
            
				_dispatcher = null;

				return true;
			}
			catch(Exception ex)
			{
				this.OnException(this, new ExceptionEventArgs(ex));
			}        
			return false;
		}
예제 #13
0
		/// <summary>
		/// Starts the server (Incoming connections will be accepted while the server is started)
		/// </summary>
		/// <param name="ep"></param>
		/// <returns></returns>
		public bool Start(IPEndPoint ep)
		{
			try
			{
				_thread = new BackgroundThread();
				_thread.AllowThreadAbortException = true;
				_thread.Run += new BackgroundThreadStartEventHandler(OnThreadRun);
				_thread.Start(true, new object[] {ep});

				return true;
			}
			catch(Exception ex)
			{
				this.OnException(this, new ExceptionEventArgs(ex));
			}
			return false;
		}
		/// <summary>
		/// Initializes a new instance of the AddressBookItemBackgroundThreadContext class
		/// </summary>
		/// <param name="thread"></param>
		/// <param name="item"></param>
		public AddressBookItemBackgroundThreadContext(BackgroundThread thread, AddressBookItem item)
		{
			_thread = thread;
			_item = item;
		}
		/// <summary>
		/// Initializes a new instance of the BackgroundThreadEventArgs class
		/// </summary>
		/// <param name="thread">The background thread about which this event is centered</param>
		public BackgroundThreadEventArgs(BackgroundThread thread) : base()
		{
			_thread = thread;
		}
 public BackgroundThreadAlreadyExistsException(BackgroundThread thread) : base("The background thread already exists in the list.")
 {
     _thread = thread;
 }