Silverlight & XAML Playground
about XAML and other Amenities

A base class for threaded Application Services

2011-02-21T23:26:16+01:00 by Andrea Boschin

Continuing of my series about IApplicationServices I would like to propose a base class I've created to easily develop Application Services that runs a thread. There are lot of cases when you implement IApplicationService to run a parallel task that accomplish some kind of background tasks. I've collected all the redundant code inside a class I called ThreadedService. Extending this class you have a thread automatically started and stopped according with the application lifetime. Here is the class:

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
}

As already said the class is able to manage the background thread automatically but require a little level of collaboration when you develop the body of the thread. Infact after overriding the ThreadProc method you have to listen for the ExitEvent to be raised. When this event is set you have to exit from the service as soon as possible.

Saying we have to poll the newtork once every minute you can implement the ThreadProc the way I show it in the figure.

protected override void ThreadProc()
{
    this.Update();
 
    while (WaitHandle.WaitAny(this.ExitHandles, 5000) == WaitHandle.WaitTimeout)
        this.Update();
}

The procedure wait for the ExitEvent for a timeout. During this time the runtime is able to do something else but once the event is set the process is immediately exited. Something very simple but effective.

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed