额外的类字典来管理多个动作

时间:2017-11-09 02:36:20

标签: c#

QueueTask类用于运行带队列的操作。我想创建多个队列并处理作为较低任务的队列,然后在那里添加队列。

此外,它应该能够自动创建新队列,取消和删除。

但是,队列方法效率似乎有点低,可以改进什么?

public class QueueTaskExtra
{
    private static int m_Counter = 0;
    private Dictionary<int, QueueTask> QueuePool = new Dictionary<int, QueueTask>();
    public int QueuePoolID { get; private set; } = 0;

    public QueueTaskExtra()
    {
        this.QueuePoolID = System.Threading.Interlocked.Increment(ref m_Counter);
        QueuePool.Add(QueuePoolID, new QueueTask());
    }

    public void NewQueue()
    {
        this.QueuePoolID = System.Threading.Interlocked.Increment(ref m_Counter);
        QueuePool.Add(QueuePoolID, new QueueTask());
    }

    public void Queue(ActionInfo ai)
    {
        QueueTask qt = new QueueTask();
        int tmpCount = 999999999;
        foreach (var qf in QueuePool)
        {
            if(qf.Value.Count() < tmpCount)
            {
                qt = qf.Value;
                tmpCount = qf.Value.Count();
            }
        }
        qt.Queue(ai);
    }
}

0 个答案:

没有答案