public abstract class ThreadedService : IDisposable, IApplicationService, IApplicationLifetimeAware
{
/// <summary>
/// Gets or sets the exit event.
/// </summary>
/// <value>The exit event.</value>
private ManualResetEvent ExitEvent { get; set; }
/// <summary>
/// Gets or sets the wait handles.
/// </summary>
/// <value>The wait handles.</value>
protected WaitHandle[] ExitHandles { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ThreadedObject"/> class.
/// </summary>
public ThreadedService()
{
this.ExitEvent = new ManualResetEvent(false);
this.ExitHandles = new WaitHandle[] { this.ExitEvent };
}
/// <summary>
/// The main thread body
/// </summary>
protected abstract void ThreadProc();
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public virtual void Dispose()
{
this.StopService();
}
#endregion
#region IApplicationLifetimeAware
public virtual void Starting()
{ }
public virtual void Started()
{
ThreadStart listenThreadStart = new ThreadStart(ThreadProc);
Thread thread = new Thread(listenThreadStart);
thread.Start();
}
public virtual void Exiting()
{
this.ExitEvent.Set();
}
public virtual void Exited()
{ }
#endregion
#region IApplicationService
public virtual void StartService(ApplicationServiceContext context)
{ }
public virtual void StopService()
{ }
#endregion
}