/// <summary>
        /// Добавление нового инстанса, если его нет
        /// </summary>
        /// <param name="instanceName">Имя инстанса</param>
        /// <returns>Был ли добавлен (false, если уже существовал)</returns>
        private bool TryAddInstance(string instanceName)
        {
            if (_instances.ContainsKey(instanceName))
            {
                return(false);
            }

            var inst = new WinInstanceInMultiInstanceCategory(this, instanceName);

            if (!_instances.TryAdd(instanceName, inst))
            {
                return(false);
            }

            inst.Init();
            return(true);
        }
        /// <summary>
        /// Получение или создание инстанса
        /// </summary>
        /// <param name="instanceName">Имя инстанса</param>
        /// <returns>Инстанс</returns>
        public override InstanceInMultiInstanceCategory this[string instanceName]
        {
            get
            {
                WinInstanceInMultiInstanceCategory result = null;
                if (!_instances.TryGetValue(instanceName, out result))
                {
                    result = _instances.GetOrAdd(instanceName, new WinInstanceInMultiInstanceCategory(this, instanceName));
                }

                if (result.State == WinCategoryState.Created && State == WinCategoryState.Initialized)
                {
                    result.Init();
                }

                return(result);
            }
        }
        /// <summary>
        /// Инициализация категории
        /// </summary>
        protected override void InitCategory()
        {
            CounterCreationDataCollection collection = new CounterCreationDataCollection();

            foreach (var counter in _counters)
            {
                counter.Value.FillCounterCreationData(collection);
            }

            if (HasWinCategory())
            {
                if (Info.InstantiationMode == WinCountersInstantiationMode.AlwaysCreateNew)
                {
                    DeleteWinCategory();
                    _winCategory = CreateWinCategory(collection);
                }
                else if (Info.InstantiationMode == WinCountersInstantiationMode.UseExistedIfPossible)
                {
                    var wc = GetExistedWinCategory();
                    var existedCounters = wc.GetCounters("");
                    if (wc.CategoryType != PerformanceCounterCategoryType.MultiInstance ||
                        !CategoryHelper.IsAllRequestedCountersExist(existedCounters, collection))
                    {
                        DeleteWinCategory();
                        _winCategory = CreateWinCategory(collection);
                    }
                    else
                    {
                        _winCategory = wc;

                        if (Info.ExistedInstancesTreatment == WinCountersExistedInstancesTreatment.LoadExisted)
                        {
                            foreach (var instName in wc.GetInstanceNames())
                            {
                                TryAddInstance(instName);
                            }
                        }
                        else if (Info.ExistedInstancesTreatment == WinCountersExistedInstancesTreatment.RemoveExisted)
                        {
                            foreach (var instName in wc.GetInstanceNames())
                            {
                                if (!HasInstance(instName))
                                {
                                    WinInstanceInMultiInstanceCategory inst = new WinInstanceInMultiInstanceCategory(this, instName);
                                    inst.Init();
                                    inst.OnRemoveFromMultiInstanceCategory(true);
                                }
                            }
                        }
                    }

                    foreach (var cnt in existedCounters)
                    {
                        cnt.Dispose();
                    }
                }
                else
                {
                    throw new InvalidOperationException("Incorrect WinCountersInstantiationMode value: " + Info.InstantiationMode.ToString());
                }
            }
            else
            {
                _winCategory = CreateWinCategory(collection);
            }

            foreach (var inst in _instances)
            {
                inst.Value.Init();
            }
        }