Click here to Skip to main content
15,896,727 members
Articles / Programming Languages / C#

Applied Use of LinFu/Cecil and Aspect-Oriented Programming Concepts - A Library

Rate me:
Please Sign up or sign in to vote.
4.88/5 (8 votes)
4 Sep 2008CPOL17 min read 35.2K   160   32  
A library of useful functionality using Aspect-Oriented Programming concepts, and implemented using the LinFu and Cecil.Mono projects/frameworks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using BrainTechLLC.ThreadSafeObjects;
using Hyper.ComponentModel;

namespace BrainTechLLC.ThreadSafeObjects
{
    /// <summary>
    /// A simple queue that avoids using lock().  Fast/efficient to dequeue/enqueue from multiple threads
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [TypeDescriptionProvider(typeof(HyperTypeDescriptionProvider)), Serializable]
    public class ThreadSafeQueue<TListType> : Lockable, IMultipleItems<TListType>, ISupportsCount, ISupportsEnqueueDequeue<TListType> where TListType : class
    {
        public Queue<TListType> _queue = new Queue<TListType>();
        public int _count;

        public List<TListType> AllItems { get { return new List<TListType>(ArrayOfItems); } }
        public int Count { get { return _count; } }

        public void Enqueue(TListType item)
        {
            // We need to be sure that no other threads simultaneously modify the shared _queue
            // object during our enqueue operation
            AquireLock();
            {
                _queue.Enqueue(item);
                _count++;
            }
            ReleaseLock();
        }

        public void EnqueueMultiple(List<TListType> items)
        {
            AquireLock();
            {
                foreach (TListType item in items)
                    _queue.Enqueue(item);

                _count += items.Count;
            }
            ReleaseLock();
        }

        public int DequeueMultiple(List<TListType> items, int maxItems)
        {
            int dequeued = 0;

            AquireLock();
            {
                while (_count > 0 && dequeued < maxItems)
                {
                    TListType item = _queue.Dequeue();
                    items.Add(item);
                    _count--;
                    dequeued++;
                }
            }
            ReleaseLock();

            return dequeued;
        }

        public int DequeueMultiple(TListType[] items, int maxItems)
        {
            int dequeued = 0;

            AquireLock();
            {
                while (_count > 0 && dequeued < maxItems)
                {
                    TListType item = _queue.Dequeue();
                    items[dequeued] = item;
                    dequeued++;
                    _count--;
                }
            }
            ReleaseLock();

            return dequeued;
        }


        public bool Dequeue(out TListType item)
        {
            item = null;

            // We need to be sure that no other threads simultaneously modify the shared _queue
            // object during our dequeue operation
            AquireLock();
            {
                if (_count > 0)
                {
                    item = _queue.Dequeue();
                    _count--;
                }
            }
            ReleaseLock();

            return (item != null);
        }

        public bool IsInList(TListType item)
        {
            bool found;

            AquireLock();
            {
                found = _queue.Contains(item);
            }
            ReleaseLock();

            return found;
        }

        public TListType[] ArrayOfItems
        {
            get
            {
                TListType[] list;

                AquireLock();
                {
                    list = _queue.ToArray();
                }
                ReleaseLock();

                return list;
            }
        }

        public virtual void Clear()
        {
            AquireLock();
            {
                _queue.Clear();
                _count = 0;
            }
            ReleaseLock();
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior) Troppus Software
United States United States
Currently working as a Senior Silverlight Developer with Troppus Software in Superior, CO. I enjoy statistics, programming, new technology, playing the cello, and reading codeproject articles. Smile | :)

Comments and Discussions