Beispiel #1
0
        public CounterSample NextSample()
        {
            string categoryName = this.categoryName;
            string machineName  = this.machineName;

            new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Browse, machineName, categoryName).Demand();
            this.Initialize();
            CategorySample          categorySample          = PerformanceCounterLib.GetCategorySample(machineName, categoryName);
            CounterDefinitionSample counterDefinitionSample = categorySample.GetCounterDefinitionSample(this.counterName);

            this.counterType = counterDefinitionSample.CounterType;
            if (!categorySample.IsMultiInstance)
            {
                if ((this.instanceName != null) && (this.instanceName.Length != 0))
                {
                    throw new InvalidOperationException(SR.GetString("InstanceNameProhibited", new object[] { this.instanceName }));
                }
                return(counterDefinitionSample.GetSingleValue());
            }
            if ((this.instanceName == null) || (this.instanceName.Length == 0))
            {
                throw new InvalidOperationException(SR.GetString("InstanceNameRequired"));
            }
            return(counterDefinitionSample.GetInstanceValue(this.instanceName));
        }
Beispiel #2
0
        // Will cause an update, raw value
        /// <include file='doc\PerformanceCounter.uex' path='docs/doc[@for="PerformanceCounter.NextSample"]/*' />
        /// <devdoc>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </devdoc>
        public CounterSample NextSample()
        {
            Initialize();
            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(this.machineName, this.categoryName);

            try {
                CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(this.counterName);
                this.counterType = counterSample.CounterType;
                if (counterSample.IsSingleInstance)
                {
                    if (instanceName != null && instanceName.Length != 0)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.InstanceNameProhibited, this.instanceName));
                    }

                    return(counterSample.GetSingleValue());
                }
                else
                {
                    if (instanceName == null || instanceName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.InstanceNameRequired));
                    }

                    return(counterSample.GetInstanceValue(this.instanceName));
                }
            }
            finally {
                categorySample.Dispose();
            }
        }
        // Will cause an update, raw value
        /// <summary>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </summary>
        public CounterSample NextSample()
        {
            string currentCategoryName = _categoryName;
            string currentMachineName  = _machineName;

            Initialize();


            using (CategorySample categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName))
            {
                CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(_counterName);
                _counterType = counterSample._counterType;
                if (!categorySample._isMultiInstance)
                {
                    if (_instanceName != null && _instanceName.Length != 0)
                    {
                        throw new InvalidOperationException(SR.Format(SR.InstanceNameProhibited, _instanceName));
                    }

                    return(counterSample.GetSingleValue());
                }
                else
                {
                    if (_instanceName == null || _instanceName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.InstanceNameRequired);
                    }

                    return(counterSample.GetInstanceValue(_instanceName));
                }
            }
        }
        // Will cause an update, raw value
        /// <devdoc>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </devdoc>
        public CounterSample NextSample()
        {
            string currentCategoryName = categoryName;
            string currentMachineName  = machineName;

            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, currentMachineName, currentCategoryName);

            permission.Demand();

            Initialize();
            CategorySample          categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName);
            CounterDefinitionSample counterSample  = categorySample.GetCounterDefinitionSample(this.counterName);

            this.counterType = counterSample.CounterType;
            if (!categorySample.IsMultiInstance)
            {
                if (instanceName != null && instanceName.Length != 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.InstanceNameProhibited, this.instanceName));
                }

                return(counterSample.GetSingleValue());
            }
            else
            {
                if (instanceName == null || instanceName.Length == 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.InstanceNameRequired));
                }

                return(counterSample.GetInstanceValue(this.instanceName));
            }
        }