/// <summary>
        ///     Intializes required resources
        /// </summary>
        private void InitializeImpl()
        {
            bool tookLock = false;

            try
            {
                Monitor.Enter(InstanceLockObject, ref tookLock);

                if (!_initialized)
                {
                    string currentCategoryName = _categoryName;
                    string currentMachineName  = _machineName;

                    if (currentCategoryName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.CategoryNameMissing);
                    }
                    if (_counterName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.CounterNameMissing);
                    }

                    if (ReadOnly)
                    {
                        if (!PerformanceCounterLib.CounterExists(currentMachineName, currentCategoryName, _counterName))
                        {
                            throw new InvalidOperationException(SR.Format(SR.CounterExists, currentCategoryName, _counterName));
                        }

                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (_instanceLifetime != PerformanceCounterInstanceLifetime.Global)
                        {
                            throw new InvalidOperationException(SR.InstanceLifetimeProcessonReadOnly);
                        }

                        _initialized = true;
                    }
                    else
                    {
                        if (currentMachineName != "." && !string.Equals(currentMachineName, PerformanceCounterLib.ComputerName, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new InvalidOperationException(SR.RemoteWriting);
                        }

                        if (!PerformanceCounterLib.IsCustomCategory(currentMachineName, currentCategoryName))
                        {
                            throw new InvalidOperationException(SR.NotCustomCounter);
                        }

                        // check category type
                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (string.IsNullOrEmpty(_instanceName) && InstanceLifetime == PerformanceCounterInstanceLifetime.Process)
                        {
                            throw new InvalidOperationException(SR.InstanceLifetimeProcessforSingleInstance);
                        }

                        _sharedCounter = new SharedPerformanceCounter(currentCategoryName.ToLowerInvariant(), _counterName.ToLowerInvariant(), _instanceName.ToLowerInvariant(), _instanceLifetime);
                        _initialized   = true;
                    }
                }
            }
            finally
            {
                if (tookLock)
                {
                    Monitor.Exit(InstanceLockObject);
                }
            }
        }
Beispiel #2
0
        private void InitializeImpl()
        {
            bool lockTaken = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                Monitor.Enter(this.InstanceLockObject, ref lockTaken);
                if (!this.initialized)
                {
                    string categoryName = this.categoryName;
                    string machineName  = this.machineName;
                    if (categoryName == string.Empty)
                    {
                        throw new InvalidOperationException(SR.GetString("CategoryNameMissing"));
                    }
                    if (this.counterName == string.Empty)
                    {
                        throw new InvalidOperationException(SR.GetString("CounterNameMissing"));
                    }
                    if (this.ReadOnly)
                    {
                        new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Browse, machineName, categoryName).Demand();
                        if (!PerformanceCounterLib.CounterExists(machineName, categoryName, this.counterName))
                        {
                            throw new InvalidOperationException(SR.GetString("CounterExists", new object[] { categoryName, this.counterName }));
                        }
                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(machineName, categoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(this.instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString("MultiInstanceOnly", new object[] { categoryName }));
                            }
                        }
                        else if ((categoryType == PerformanceCounterCategoryType.SingleInstance) && !string.IsNullOrEmpty(this.instanceName))
                        {
                            throw new InvalidOperationException(SR.GetString("SingleInstanceOnly", new object[] { categoryName }));
                        }
                        if (this.instanceLifetime != PerformanceCounterInstanceLifetime.Global)
                        {
                            throw new InvalidOperationException(SR.GetString("InstanceLifetimeProcessonReadOnly"));
                        }
                        this.initialized = true;
                    }
                    else
                    {
                        new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Write, machineName, categoryName).Demand();
                        if ((machineName != ".") && (string.Compare(machineName, PerformanceCounterLib.ComputerName, StringComparison.OrdinalIgnoreCase) != 0))
                        {
                            throw new InvalidOperationException(SR.GetString("RemoteWriting"));
                        }
                        SharedUtils.CheckNtEnvironment();
                        if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                        {
                            throw new InvalidOperationException(SR.GetString("NotCustomCounter"));
                        }
                        PerformanceCounterCategoryType type2 = PerformanceCounterLib.GetCategoryType(machineName, categoryName);
                        if (type2 == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(this.instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString("MultiInstanceOnly", new object[] { categoryName }));
                            }
                        }
                        else if ((type2 == PerformanceCounterCategoryType.SingleInstance) && !string.IsNullOrEmpty(this.instanceName))
                        {
                            throw new InvalidOperationException(SR.GetString("SingleInstanceOnly", new object[] { categoryName }));
                        }
                        if (string.IsNullOrEmpty(this.instanceName) && (this.InstanceLifetime == PerformanceCounterInstanceLifetime.Process))
                        {
                            throw new InvalidOperationException(SR.GetString("InstanceLifetimeProcessforSingleInstance"));
                        }
                        this.sharedCounter = new SharedPerformanceCounter(categoryName.ToLower(CultureInfo.InvariantCulture), this.counterName.ToLower(CultureInfo.InvariantCulture), this.instanceName.ToLower(CultureInfo.InvariantCulture), this.instanceLifetime);
                        this.initialized   = true;
                    }
                }
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(this.InstanceLockObject);
                }
            }
        }
        /// <devdoc>
        ///     Intializes required resources
        /// </devdoc>
        //[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        private void InitializeImpl()
        {
            bool tookLock = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                Monitor.Enter(InstanceLockObject, ref tookLock);

                if (!initialized)
                {
                    string currentCategoryName = categoryName;
                    string currentMachineName  = machineName;

                    if (currentCategoryName == String.Empty)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.CategoryNameMissing));
                    }
                    if (this.counterName == String.Empty)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.CounterNameMissing));
                    }

                    if (this.ReadOnly)
                    {
                        PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, currentMachineName, currentCategoryName);

                        permission.Demand();

                        if (!PerformanceCounterLib.CounterExists(currentMachineName, currentCategoryName, counterName))
                        {
                            throw new InvalidOperationException(SR.GetString(SR.CounterExists, currentCategoryName, counterName));
                        }

                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (String.IsNullOrEmpty(instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!String.IsNullOrEmpty(instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (instanceLifetime != PerformanceCounterInstanceLifetime.Global)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.InstanceLifetimeProcessonReadOnly));
                        }

                        this.initialized = true;
                    }
                    else
                    {
                        PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Write, currentMachineName, currentCategoryName);
                        permission.Demand();

                        if (currentMachineName != "." && String.Compare(currentMachineName, PerformanceCounterLib.ComputerName, StringComparison.OrdinalIgnoreCase) != 0)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.RemoteWriting));
                        }

                        SharedUtils.CheckNtEnvironment();

                        if (!PerformanceCounterLib.IsCustomCategory(currentMachineName, currentCategoryName))
                        {
                            throw new InvalidOperationException(SR.GetString(SR.NotCustomCounter));
                        }

                        // check category type
                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (String.IsNullOrEmpty(instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!String.IsNullOrEmpty(instanceName))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (String.IsNullOrEmpty(instanceName) && InstanceLifetime == PerformanceCounterInstanceLifetime.Process)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.InstanceLifetimeProcessforSingleInstance));
                        }

                        this.sharedCounter = new SharedPerformanceCounter(currentCategoryName.ToLower(CultureInfo.InvariantCulture), counterName.ToLower(CultureInfo.InvariantCulture), instanceName.ToLower(CultureInfo.InvariantCulture), instanceLifetime);
                        this.initialized   = true;
                    }
                }
            } finally {
                if (tookLock)
                {
                    Monitor.Exit(InstanceLockObject);
                }
            }
        }