吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1343|回复: 7
收起左侧

[求助] C#如何使用PerformanceCounter获取GPU利用率

  [复制链接]
一只小凡凡 发表于 2022-9-17 17:55
开发游戏模式,通过检测GPU利用率来开启游戏模式,百度出可以使用PerformanceCounter,但是技术有限,希望大佬教教我!

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wjshan0808 发表于 2022-11-17 15:36
你可以参考一下如下代码
[C#] 纯文本查看 复制代码
        /// <summary>
        /// 获取利用率
        /// </summary>
        /// <param name="lstCounters">计数集合</param>
        /// <returns>利用率</returns>
        public float GetUsage(List<PerformanceCounter> lstCounters)
        {
            if (null == lstCounters)
            {
                throw new ArgumentNullException("Parameter 'Counters' is null.");
            }

            //计算利用率
            lstCounters.ForEach(a => a.NextValue());
            Thread.Sleep(0x400);
            return lstCounters.Sum(a => a.NextValue());
        }
        /// <summary>
        /// 获取计数集合
        /// </summary>
        /// <param name="oCategory">种类</param>
        /// <param name="iPID">进程ID</param>
        /// <returns>计数集合</returns>
        public List<PerformanceCounter> GetEngtypeCounters(PerformanceCounterCategory oCategory, int iPID = -1)
        {
            if (null == oCategory)
            {
                throw new ArgumentNullException("Parameter 'Category' is null.");
            }

            //获取指定的实例名
            List<string> lstInstanceNames = new List<string>();
            {
                lstInstanceNames.AddRange(oCategory.GetInstanceNames());
            };
            //过滤
            {
                //排除非指定的实例名
                if (iPID != -1)
                {
                    lstInstanceNames.RemoveAll(p => !p.StartsWith(string.Format("pid_{0}", iPID)));
                }
                //排除非指定的实例名类型
                {
                    lstInstanceNames.RemoveAll(p => !p.EndsWith("engtype_3D"));
                }
            }

            //调试
            {
                foreach (var item in lstInstanceNames)
                {
                    PerformanceCounter[] arr = oCategory.GetCounters(item);
                    foreach (var itemP in arr)
                    {
                        //Console.WriteLine(itemP.CounterName);
                    }
                }
            }

            //获取利用率实例项
            return lstInstanceNames.SelectMany(f => oCategory.GetCounters(f)).Where(f => "Utilization Percentage" == f.CounterName).ToList();
        }

        /// <summary>
        /// 获取内存计数集合
        /// </summary>
        /// <param name="oCategory">种类</param>
        /// <param name="iPID">进程ID</param>
        /// <returns>内存计数集合</returns>
        public List<PerformanceCounter> GetMemoryCounters(PerformanceCounterCategory oCategory, int iPID = -1)
        {
            if (null == oCategory)
            {
                throw new ArgumentNullException("Parameter 'Category' is null.");
            }

            //获取指定的实例名
            List<string> lstInstanceNames = new List<string>();
            {
                lstInstanceNames.AddRange(oCategory.GetInstanceNames());
            };
            //过滤
            {
                //排除非指定的实例名
                if (iPID != -1)
                {
                    lstInstanceNames.RemoveAll(p => !p.StartsWith(string.Format("pid_{0}", iPID)));
                }
            }

            //调试
            {
                foreach (var item in lstInstanceNames)
                {
                    PerformanceCounter[] arr = oCategory.GetCounters(item);
                    foreach (var itemP in arr)
                    {
                        //Console.WriteLine(itemP.CounterName);
                    }
                }
            }

            //获取利用率实例项
            return lstInstanceNames.SelectMany(f => oCategory.GetCounters(f)).Where(f => "Local Usage" == f.CounterName).ToList();
        }
        /// <summary>
        /// 获取种类
        /// </summary>
        /// <param name="strCategory">种类名</param>
        /// <returns>种类</returns>
        public PerformanceCounterCategory GetCategory(string strCategory)
        {
            if (string.IsNullOrEmpty(strCategory))
            {
                throw new ArgumentNullException("Parameter 'Category' is null.");
            }

            //调试
            {
                PerformanceCounterCategory[] arr = PerformanceCounterCategory.GetCategories(".");
                foreach (var item in arr)
                {
                    //Console.WriteLine(item.CategoryName);
                }
            }

            //
            PerformanceCounterCategory oCategory = null;
            if (PerformanceCounterCategory.Exists(strCategory))
            {
                //获取存在的指定种类
                oCategory = PerformanceCounterCategory.GetCategories().FirstOrDefault(f => f.CategoryName == strCategory);
            }
            if (null == oCategory)
            {
                //创建新的指定种类
                oCategory = new PerformanceCounterCategory(strCategory);
            }

            return oCategory;
        }

wjshan0808 发表于 2022-11-17 15:38
注意
获取GPU利用率时 是直接的百分比数值
获取内存使用率时 是字节数值
 楼主| 一只小凡凡 发表于 2022-11-17 22:49
wjshan0808 发表于 2022-11-17 15:38
注意
获取GPU利用率时 是直接的百分比数值
获取内存使用率时 是字节数值

请问大佬,这是怎么判断是AMD,Intel,Nevada的显卡?
wjshan0808 发表于 2022-11-18 09:37
你是问 你程序用的是那个显卡,还是你电脑上装的是什么显卡
 楼主| 一只小凡凡 发表于 2022-11-18 09:56
wjshan0808 发表于 2022-11-18 09:37
你是问 你程序用的是那个显卡,还是你电脑上装的是什么显卡

大佬能做个demo吗
wjshan0808 发表于 2022-11-18 12:11
网上用C#获取显卡信息的只找到了下面一段代码,希望对你有所帮助
[C#] 纯文本查看 复制代码
ManagementObjectSearcher myVideoObject = new ManagementObjectSearcher("select * from Win32_VideoController");

foreach (ManagementObject obj in myVideoObject.Get())
{
    Console.WriteLine("Name  -  " + obj["Name"]);
    Console.WriteLine("Status  -  " + obj["Status"]);
    Console.WriteLine("Caption  -  " + obj["Caption"]);
    Console.WriteLine("DeviceID  -  " + obj["DeviceID"]);
    Console.WriteLine("AdapterRAM  -  " + obj["AdapterRAM"]);
    Console.WriteLine("AdapterDACType  -  " + obj["AdapterDACType"]);
    Console.WriteLine("Monochrome  -  " + obj["Monochrome"]);
    Console.WriteLine("InstalledDisplayDrivers  -  " + obj["InstalledDisplayDrivers"]);
    Console.WriteLine("DriverVersion  -  " + obj["DriverVersion"]);
    Console.WriteLine("VideoProcessor  -  " + obj["VideoProcessor"]);
    Console.WriteLine("VideoArchitecture  -  " + obj["VideoArchitecture"]);
    Console.WriteLine("VideoMemoryType  -  " + obj["VideoMemoryType"]);
}

参考连接 https://ourcodeworld.com/articles/read/294/how-to-retrieve-basic-and-advanced-hardware-and-software-information-gpu-hard-drive-processor-os-printers-in-winforms-with-c-sharp
 楼主| 一只小凡凡 发表于 2022-11-18 19:08
wjshan0808 发表于 2022-11-18 12:11
网上用C#获取显卡信息的只找到了下面一段代码,希望对你有所帮助
[mw_shl_code=csharp,true]ManagementObj ...

OK,我去看看
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 07:06

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表