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)
{
foreach (Func<T, E> pFun in pFuncs)
{
T args = pParmters.Dequeue();
Action<E> pFunction = pActions.Dequeue();
pFun.BeginInvoke(args, new AsyncCallback((asyobj)=>
{
if (asyobj.IsCompleted)
{
Action<E> pTempFunction = asyobj.AsyncState as Action<E>;
E result = pFun.EndInvoke(asyobj);
pTempFunction(result);
}
}),pFunction);
//pFunction.BeginInvoke(result,null,null);
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;
}
}
MyQueue Async
最新推荐文章于 2022-12-23 17:56:08 发布