Example #1
0
        /// <summary>
        /// Create counter list from string
        /// </summary>
        /// <param name="counterString">string which represents one or more counters: "\\<machineName>\<categoryName>(<instanceName>)\<counterName>"
        /// counterName and/or instanceName can have the special value "#ALL#", meaning we want to get all of them</param>
        /// <returns>list of counters</returns>
        public static IEnumerable <System.Diagnostics.PerformanceCounter> CreateCountersFromString(string counterString)
        {
            string machineName, categoryName, instanceName, counterName;

            ParseCounterString(counterString, out machineName, out categoryName, out instanceName, out counterName);

            System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory(categoryName, machineName);

            IEnumerable <System.Diagnostics.PerformanceCounter> counters = new System.Diagnostics.PerformanceCounter[] { };

            if (counterName == "#ALL#" && instanceName == "#ALL#")
            {
                foreach (string instance in category.GetInstanceNames().OrderBy(s => s))
                {
                    counters = counters.Concat(category.GetCounters(instance));
                }
            }
            else if (counterName == "#ALL#")
            {
                if (string.IsNullOrEmpty(instanceName))
                {
                    counters = category.GetCounters();
                }
                else
                {
                    counters = category.GetCounters(instanceName);
                }
            }
            else if (instanceName == "#ALL#")
            {
                foreach (string instance in category.GetInstanceNames().OrderBy(s => s))
                {
                    counters = counters.Concat(new System.Diagnostics.PerformanceCounter[] { new System.Diagnostics.PerformanceCounter(categoryName, counterName, instance, machineName) });
                }
            }
            else
            {
                counters = new System.Diagnostics.PerformanceCounter[] { new System.Diagnostics.PerformanceCounter(categoryName, counterName, instanceName, machineName) };
            }

            // Création des contrôles
            return(counters);
        }
        public static System.Collections.Generic.Dictionary <int, int> GetAllProcessParentPids()
        {
            var childPidToParentPid = new System.Collections.Generic.Dictionary <int, int>();

            var processCounters = new System.Collections.Generic.SortedDictionary <string, System.Diagnostics.PerformanceCounter[]>();
            var category        = new System.Diagnostics.PerformanceCounterCategory("Process");

            // As the base system always has more than one process running,
            // don't special case a single instance return.
            var instanceNames = category.GetInstanceNames();

            foreach (string t in instanceNames)
            {
                try
                {
                    processCounters[t] = category.GetCounters(t);
                }
                catch (System.InvalidOperationException)
                {
                    // Transient processes may no longer exist between
                    // GetInstanceNames and when the counters are queried.
                }
            }

            foreach (var kvp in processCounters)
            {
                int childPid  = -1;
                int parentPid = -1;

                foreach (var counter in kvp.Value)
                {
                    if ("ID Process".CompareTo(counter.CounterName) == 0)
                    {
                        childPid = (int)(counter.NextValue());
                    }
                    else if ("Creating Process ID".CompareTo(counter.CounterName) == 0)
                    {
                        parentPid = (int)(counter.NextValue());
                    }
                }

                if (childPid != -1 && parentPid != -1)
                {
                    childPidToParentPid[childPid] = parentPid;
                }
            }

            return(childPidToParentPid);
        } // End Function GetAllProcessParentPids
Example #3
0
        private void instanceCb_SelectedIndexChanged(object sender, EventArgs e)
        {
            nameCb.Items.Clear();
            nameCb.Text    = "";
            nameCb.Enabled = true;

            var category = new System.Diagnostics.PerformanceCounterCategory(categoryCb.SelectedItem.ToString());

            System.Collections.ArrayList counters = new System.Collections.ArrayList();
            counters.AddRange(category.GetCounters(instanceCb.Text.ToString()));

            foreach (System.Diagnostics.PerformanceCounter counter in counters)
            {
                nameCb.Items.Add(counter.CounterName);
            }
        }
Example #4
0
        private void categoryCb_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] instanceNames;

            var category = new System.Diagnostics.PerformanceCounterCategory(categoryCb.SelectedItem.ToString());

            instanceCb.Items.Clear();
            nameCb.Items.Clear();
            instanceCb.Text    = "";
            nameCb.Text        = "";
            instanceCb.Enabled = true;
            nameCb.Enabled     = false;

            try
            {
                instanceNames = category.GetInstanceNames();
                if (instanceNames.Length == 0)
                {
                    instanceCb.Enabled = false;
                    nameCb.Enabled     = true;

                    System.Collections.ArrayList counters = new System.Collections.ArrayList();
                    counters.AddRange(category.GetCounters());

                    foreach (System.Diagnostics.PerformanceCounter counter in counters)
                    {
                        nameCb.Items.Add(counter.CounterName);
                    }
                }
                else
                {
                    for (int i = 0; i < instanceNames.Length; i++)
                    {
                        instanceCb.Items.Add(instanceNames[i]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Unable to list the counters for this category:\n" + ex.Message);
            }
        }
Example #5
0
        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Collections.ArrayList counters = new System.Collections.ArrayList();

            if (this.listBox2.SelectedIndex != -1)
            {
                System.Diagnostics.PerformanceCounterCategory selectedPerfCat = new System.Diagnostics.PerformanceCounterCategory(this.listBox1.SelectedItem.ToString());
                string selectedInstance = this.listBox2.SelectedItem.ToString();
                this.listBox3.Items.Clear();

                try
                {
                    counters.AddRange(selectedPerfCat.GetCounters(selectedInstance));

                    foreach (System.Diagnostics.PerformanceCounter counter in counters)
                    {
                        this.listBox3.Items.Add(counter.CounterName);
                    }

                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Unable to list the counters for this category:\n" + ex.Message);
                }
            }
        }
        /// <summary>
        /// Create counter list from string
        /// </summary>
        /// <param name="counterString">string which represents one or more counters: "\\<machineName>\<categoryName>(<instanceName>)\<counterName>"
        /// counterName and/or instanceName can have the special value "#ALL#", meaning we want to get all of them</param>
        /// <returns>list of counters</returns>
        public static IEnumerable<System.Diagnostics.PerformanceCounter> CreateCountersFromString(string counterString)
        {
            string machineName, categoryName, instanceName, counterName;
            
            ParseCounterString(counterString, out machineName, out categoryName, out instanceName, out counterName);

            System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory(categoryName, machineName);

            IEnumerable<System.Diagnostics.PerformanceCounter> counters = new System.Diagnostics.PerformanceCounter[] { };

            if (counterName == "#ALL#" && instanceName == "#ALL#")
            {
                foreach (string instance in category.GetInstanceNames().OrderBy(s => s))
                {
                    counters = counters.Concat(category.GetCounters(instance));
                }
            }
            else if (counterName == "#ALL#")
            {
                if (string.IsNullOrEmpty(instanceName))
                {
                    counters = category.GetCounters();
                }
                else
                {
                    counters = category.GetCounters(instanceName);
                }
            }
            else if (instanceName == "#ALL#")
            {
                foreach (string instance in category.GetInstanceNames().OrderBy(s => s))
                {
                    counters = counters.Concat(new System.Diagnostics.PerformanceCounter[] { new System.Diagnostics.PerformanceCounter(categoryName, counterName, instance, machineName) });
                }
            }
            else
            {
                counters = new System.Diagnostics.PerformanceCounter[] { new System.Diagnostics.PerformanceCounter(categoryName, counterName, instanceName, machineName) };
            }

            // Création des contrôles
            return counters;
        }