Geeks With Blogs
.NET Corner Jeans, .NET and Physics (eka The Quantum Boy)
This is a post after a long hibernation. Often in our product we need worker threads performing a given action when signaled. Thread pool threads (modified ThreadPool class, not the Microsoft supplied one) may not be ideal for this as these are rather foreground, "active" operations in contrast to the background callback model ThreadPool usually projects. Observing the repeating nature of such threads, I decided to patternize this model having  some resemblance to the IRunnable interface of Java. I've chosen a simpler implementation for ActiveObjects. More complex implementations can be found over net - here for example.

     /// <summary>
    /// Active Object (runnable) interface
    /// </summary>
    public interface IActiveObject
    {
        /// <summary>
        /// Initialize an active object
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        void Initialize(string name, Action action);

        /// <summary>
        /// Signal the active object to perform its loop action.
        /// </summary>
        /// <remarks>
        /// Application may call this after some simple or complex condition evaluation
        /// </remarks>
        void Signal();

        /// <summary>
        /// Signals to shotdown this active object
        /// </summary>
        void Shutdown();
    }

Abiding by good design principles, the interface enforces a "shutdown" / "soft abort" mechanism for the active objects. A KISS implementation of the above interface is given beneath,

/// <summary>
    /// Implements a simple active object pattern implementation
    /// </summary>
    /// <remarks>
    /// Although there exists a vast number of active objects patterns (in Java they are just "runnable")
    /// scattered, one of the best I found is located at http://blog.gurock.com/wp-content/uploads/2008/01/activeobjects.pdf
    /// </remarks>
    public class ActiveObject : IActiveObject
    {
        /// <summary>
        /// Name of this active object
        /// </summary>
        private string m_Name;
       
        /// <summary>
        /// Underlying active thread
        /// </summary>
        private Thread m_ActiveThreadContext;

        /// <summary>
        /// Abstracted action that the active thread executes
        /// </summary>
        private Action m_ActiveAction;

        /// <summary>
        /// Primary signal object for this active thread.
        /// See the Signal() method for more.
        /// </summary>
        private AutoResetEvent m_SignalObject;

        /// <summary>
        /// Signal object for shutting down this active object
        /// </summary>
        private ManualResetEvent m_ShutdownEvent;

        /// <summary>
        /// Interal array of signal objects combining primary signal object and
        /// shutdown signal object
        /// </summary>
        private WaitHandle[] m_SignalObjects;
       
        public ActiveObject()
        {
        }

        public void Initialize(string name, Action action)
        {
            m_Name = name;
            m_ActiveAction = action;
            m_SignalObject = new AutoResetEvent(false);
            m_ShutdownEvent = new ManualResetEvent(false);
            m_SignalObjects = new WaitHandle[]
                                {
                                    m_ShutdownEvent,
                                    m_SignalObject
                                };

            m_ActiveThreadContext = new Thread(Run);
            m_ActiveThreadContext.Name = string.Concat("ActiveObject.", m_Name);
            m_ActiveThreadContext.Start();
        }
       
        private bool Guard()
        {
            int index = WaitHandle.WaitAny(m_SignalObjects);
            return index == 0 ? false : true;
        }
       
        /// <summary>
        /// Signal the active object to perform its loop action.
        /// </summary>
        /// <remarks>
        /// Application may call this after some simple of complex condition evaluation
        /// </remarks>
        public void Signal()
        {
            m_SignalObject.Set();
        }
       
        /// <summary>
        /// Signals to shotdown this active object
        /// </summary>
        public void Shutdown()
        {
            m_ShutdownEvent.Set();
           
            if (m_ActiveThreadContext != null)
            {
                m_ActiveThreadContext.Join();
            }
           
            m_ActiveThreadContext = null;
        }
       
        /// <summary>
        /// Core run method of this active thread
        /// </summary>
        private void Run()
        {
            try
            {
                while (Guard())
                {
                    try
                    {
                        m_ActiveAction();
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(new LogData(string.Format("ActiveObject::Run - Name: {0}, Loop Error: {1}",
                                                               m_Name,
                                                               ex.Message),
                                             Component.WebAstra,
                                             LogLevel.Error));
                    }
                }
            }
            catch(Exception ex)
            {
                Logger.Write(new LogData(string.Format("ActiveObject::Run - Name: {0}, Error: {1}",
                                                       m_Name,
                                                       ex.Message),
                                     Component.WebAstra,
                                     LogLevel.Error));
            }
            finally
            {
                m_SignalObject.Close();
                m_ShutdownEvent.Close();
               
                m_SignalObject = null;
                m_ShutdownEvent = null;
            }
        }
    }

The module/functional entity that requires to be "active" can compose the ActiveObject within it and provide an appropriate Action delegate.

public class EntityAgent
{
    private IActiveObject m_PickupActiveObject;
  
    public EntityAgent(...)
    {
        m_PickupActiveObject = new ActiveObject();
    }

    public void Initialize()
    {
        m_PickupActiveObject.Initialize("Pickup", TryPickupInternalAsync);
    }

    public void SomeComplexConditionEvaluation()
    {
       //...

       m_PickupActiveObject.Signal();
    }

    private void TryPickupInternalAsync()
    {
         //Your loop action here
    }
}

Patterns may not be strict GoF-patterns but most of the times are learned or devised on the fly by designers, developers and architects. I'll keep this post short as Deewali celebration is going on frenzy outside. Happy Deewali to all and keep hacking.
Posted on Saturday, October 17, 2009 7:58 AM .NET Core | Back to top


Comments on this post: C#, ActiveObject (Runnable)

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © dbose | Powered by: GeeksWithBlogs.net