コード例 #1
0
        private async Task <T> FindOrCreateLoader()
        {
            T firstFound;

            switch (BalancingMethod)
            {
            case BalancingMethods.Random:
                if (_loadingRandom == null)
                {
                    _loadingRandom = new Random();
                }

                CurrentIndex = _loadingRandom.Next(0, ItemPool.Count());
                break;

            case BalancingMethods.MaxCount:
                int maxedCount = 0;
                for (var i = 0; i < ItemPool.Count(); i++)
                {
                    var openItem = ItemPool.ElementAt(i);
                    if (openItem.ActiveCount >= openItem.MaxCount)
                    {
                        maxedCount++;
                    }
                    else
                    {
                        CurrentIndex = i;
                        break;
                    }
                }
                if (maxedCount == ItemPool.Count())
                {
                    var newItem = new T();
                    ItemPool.Add(newItem);
                    CurrentIndex = ItemPool.Count() - 1;
                }
                break;

            default:
                if (CurrentIndex >= ItemPool.Count())
                {
                    CurrentIndex = 0;
                }
                break;
            }


            firstFound = ItemPool.ElementAt(CurrentIndex);

            CurrentIndex++;

            return(await Task.FromResult(firstFound));
        }