views:

310

answers:

0

Hello, I am developing a .Net Compact framework application in C# which utilises some third party message queuing software installed on the device.

I am fairly new to this environment and was wondering if I could run a few key concepts of the architecture past some wiser eyes to see if I am on the right track or how it could be improved.

Whilst my application is running I need to keep the message queuing library running in the background so that it can raise notifications and inform my application of any incoming messages and process messages my application generates. It needs to run throughout the lifetime of my application, so I am thinking the best way to do this is run it in its own thread when my application launches?

I didn’t want to flood this post with code so I have put the sections on I think are important, please let me know if I need to clarify more.

I start the Message Process in a separate thread when the application launches like this,

[MTAThread]
static void Main()
{
    //Run the message processor in its own thread
Thread messagingThread = new Thread(new ThreadStart(msgProcessorStart));
messagingThread.Priority = ThreadPriority.Highest;
messagingThread.Start();

…..
Application.Run(splashForm);
}

private static void msgProcessorStart()
{
MessageProcessor.Start();                      
}

The MessageProcessor is a façade over the messaging library to simplify interaction and keep a single instance of it. I have posted part of it below, it raises events for non-delivery and notifies when messages are received.

public static class MessageProcessor
    {
        #region Declarations

//private static reference to the MessageProvider as specified in the configuration files.
private static readonly IMessageProcessor _messageProcessor = MessageAccess.MessageProvider;

        #endregion

        #region Constructor

        /// <summary>
        /// Static constructor, connects to the events on the messageProcessor instance which 
        /// relate to messages received, notifications received and exceptions raised.
        /// </summary>
        static MessageProcessor()
        {
            //Connect up events specifed on the IMessageProcessor interface. 
            _messageProcessor.MessageReceived += messageReceived; 
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Sends a request based data message.
        /// </summary>
        /// <typeparam name="T">Message which implements RequestBase</typeparam>
        /// <param name="message">The message to send</param>
        public static void SendMessage<T>(T message) where T : RequestBase
        {
            _messageProcessor.SendMessage(message);
        }

        /// <summary>
        /// Starts the Message Processor. 
        /// </summary>
        /// <returns>bool, true if started successfully.</returns>
        public static void Start()
        {
            _messageProcessor.Start();
        }

        #endregion

        #region Private methods

        //Registered listener of the IMessageProcessor.MessageReceived event
        private static void messageReceived(object sender, MsgEventArgs<IMessage> message)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(processMessage),(object)message.Value);
        }


        //Invoked via messageReceived.
        private static void processMessage(object msg)
        {            
            //process the message
        }

        #endregion
    }

The Start method is first called and establishes a session; we’ll then start to received notifications and be able to send messages.

When a message is received I am currently managing event processing in a separate thread via the ThreadPool, in order to continue and respond to other notifications and messages.

Does this look a reasonable approach and will it ensure that my message queuing library will be able to process in isolation from my application?

Any advice would be gratefully received, thanks for your time.