Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

A Flexible Plugin System

, 3 Sep 2008
A generic plugin system used to load and manage plugins
fadd-15373.zip
trunk
dlls
xunit.dll
Examples
Plugins
ExampleApplication.Shared
Properties
ExampleApplication
Properties
ExamplePlugin.Shared
Properties
ExamplePlugin
Properties
Fadd.Globalization.Yaml
fadd.snk
Properties
Tests
fadd
Commands
Net
Tests
fadd.snk
Globalization
Tests
Logging
Plugins
Properties
Tests
Validation
using System;
using System.Collections.Generic;

namespace Fadd.Commands
{
    /// <summary>
    /// Invokes all handlers for a command.
    /// </summary>
    internal class AsyncQueueItem
    {
        private readonly object _source;
        private readonly Command _command;
        private readonly Queue<CommandHandler> _handlers = new Queue<CommandHandler>();
        private readonly CommandManager _cmdMgr;
        private CommandHandler _current;
        private readonly AsyncQueueItemResult _result;
        private readonly AsyncCallback _callback;

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncQueueItem"/> class.
        /// </summary>
        /// <param name="source">used as "source" in the command event delegate.</param>
        /// <param name="cmd">command being executed.</param>
        /// <param name="cmdMgr">Used to check if propagation may be cancelled.</param>
        /// <param name="callBack">callback when command have been executed..</param>
        /// <param name="result">async result.</param>
        public AsyncQueueItem(object source, Command cmd, CommandManager cmdMgr, AsyncCallback callBack, AsyncQueueItemResult result)
        {
            _source = source;
            _command = cmd;
            _cmdMgr = cmdMgr;
            _result = result;
            _callback = callBack;
        }

        public void Add(CommandHandler handler)
        {
            _handlers.Enqueue(handler);
        }

        public bool BeginInvoke()
        {
            if (_handlers.Count == 0)
                return false;

            _current = _handlers.Dequeue();
            _current.BeginInvoke(_source, _result.Args, OnEndInvoke, null);
            return true;
        }

        private void OnEndInvoke(IAsyncResult ar)
        {
            if (_current.EndInvoke(ar))
                _command.SetHandled(true);

            if (_result.Args.CancelPropagation
                && _cmdMgr.InvokePropagationCancelled(_command, _current))
            {
                TriggerCompleted();
                return;
            }

            // Completed if list is empty
            if (!BeginInvoke())
                TriggerCompleted();
        }

        private void TriggerCompleted()
        {
            // Check if the unhandled event handles the command
            if (!_command.IsHandled)
                _cmdMgr.TriggerUnhandled(_command);

            _result.ToggleCompleted();
            if (_callback != null)
                _callback(_result);
                
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder Gauffin Interactive AB
Sweden Sweden
Founder of OneTrueError, a .NET service which captures, analyzes and provide possible solutions for exceptions.
 
blog | twitter
Follow on   Twitter   LinkedIn

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 3 Sep 2008
Article Copyright 2008 by jgauffin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid