MyQueue

来源:互联网 发布:linux vi 末行模式 编辑:程序博客网 时间:2024/06/07 01:51
    public class MyQueue<T,E>    {        private Queue<Func<T, E>> pFuncs = new Queue<Func<T, E>>();        private Queue<T> pParmters = new Queue<T>();        private Queue<Action<E>> pActions = new Queue<Action<E>>();        private Boolean isRun = true;        public void Init()        {            Thread thread = new Thread(new ParameterizedThreadStart((obj) =>            {                while (isRun)                {                    if (pFuncs.Count > 0)                    {                        for (Int32 iIndex = 0; iIndex < pFuncs.Count; iIndex++ )                {                   Func<T, E> pFun = pFuncs.Dequeue();                            T args = pParmters.Dequeue();                            E result = pFun(args);                            Action<E> pFunction = pActions.Dequeue();                            pFunction(result);                            if (!isRun)                                break;                        }                    }                }            })) { IsBackground = true };            thread.Start();        }        public void Add(T item, Func<T, E> pFun, Action<E> pAction)        {            pActions.Enqueue(pAction);            pFuncs.Enqueue(pFun);            pParmters.Enqueue(item);        }        public void UnInit()        {            isRun = false;        }    }