调用wmi时如何解决缓慢的加载时间(仅处理器)

时间:2019-06-20 19:25:11

标签: c# wmi

我的C#应用​​程序遇到问题。

我正在使用WMI检索有关我的CPU,GPU和RAM的一些信息。
除了CPU(Win32_Processor)之外,其他所有工具都运作良好且快速。 我有一个菜单,每个菜单都有不同的面板(CPU,RAM,GPU)。
起初,我尝试从应用程序的开头加载所有信息,但效果并不理想,加载时间非常长。
其次,我尝试在面板可见时(单击按钮后)加载它们,并且在ram和gpu面板上效果很好。
我对所有代码都使用了完全相同的代码。在这里:
我试图使CPU信息的加载速度尽可能快,但是通过搜索和尝试自己的事情,我无法实现。
我确实意识到要花费所有时间才能获取所有信息,似乎很奇怪,它需要那么多时间!
PS:单击cpu面板的按钮时的加载时间为3.5秒,而单击“规格”按钮时的加载时间仅显示maxfreq,内核数,数据宽度,线程数,当前电压以及l2,l3大小8秒。

private void getinfo(string hwclass, string syntax, string name)
{
    ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hwclass);
    foreach (ManagementObject mo in mos.Get())
    {
        string valuebro = Convert.ToString(mo[syntax]);
        if (name == "CPUNAME")
        {
            CPUNAME.Text = valuebro;
        }
        else if (name == "CPUMAXFREQ")
        {
            //CPUMAXFREQ.Text = "Clocked at " + valuebro + "Mhz";
            maxfreqbro = Convert.ToInt32(valuebro);
        }
        else if (name == "CPUCURFREQ")
        {
            curfreqbro = Convert.ToInt32(valuebro);
            CPUMAXFREQ.Text = "Frequency: " + valuebro + "Mhz";
        }
        else if (name == "ThreadC")
        {
            THREADC.Text ="Threads: " + valuebro;
        }
        else if (name == "DataW")
        {
            DataW.Text = "Data Width: " +valuebro + " bit";
        }
        else if (name == "L2Size")
        {
            l2size = Convert.ToInt32(valuebro) / 1000;
            L2Size.Text = "L2 Size:  " + l2size.ToString() + " MB";
        }
        else if (name == "L3Size")
        {
            l3size = Convert.ToInt32(valuebro) / 1000;
            L3Size.Text ="L3 Size:  "+ l3size.ToString() + " MB";
        }
        else if (name == "NOC")
        {
            NoOfCores.Text = "Cores: " + valuebro;
        }
        else if (name == "VOLT")
        {
            voltage = Convert.ToDecimal(valuebro) / 10;
            output = voltage.ToString();
            CurrentVolt.Text = "Core Voltage: " + output + " V";
        }
        else if (name == "CPUSTATUS")
        {
            CPUSTATUS.Text = "Status: " + valuebro;
        }
        else if (name == "AVAIL")
        {
            Avail.Text = "Code: " + valuebro;
        }
    }
}

(RAM)

private void raminfo(string hwclass, string syntax, string name)
{
    ManagementObjectSearcher ram = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hwclass);
    foreach (ManagementObject rm in ram.Get())
    {
        string valueram = Convert.ToString(rm[syntax]);
        if (name == "OED")
        {
            OEDRAM.Text = "" + valueram;
        }
        else if (name == "ERRINFO")
        {
            ErrInfoRam.Text = "Error code: " + valueram;
        }
        else if (name == "ERRDESC")
        {
            ErrDescRAM.Text = "" + valueram;
        }
        else if (name == "CCVOLT")
        {
            volts = Convert.ToDecimal(valueram) / 1000;
            finaloutput = volts.ToString();
            voltageram.Text = "Voltage: " +finaloutput + " V";
        }
        else if (name == "CCSPEED")
        {
            Currentspeed.Text = "Current Frequency: " + valueram + " Mhz";
        }
        else if (name == "SPEED")
        {
            speedram.Text = "Max Frequency: " + valueram + " Mhz";
        }
        else if (name == "CAPA")
        {
            Int64 capa = Convert.ToInt64(valueram) / 1024;
            Int64 final = capa / 1024;
            capacityram.Text = "Total memory: " + final + " MB";
        }
    }
}

(GPU)

private void gpuinfo(string hwclass, string syntax, string name)
{
    ManagementObjectSearcher gpu = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hwclass);
    foreach (ManagementObject gp in gpu.Get())
    {
        string valuegpu = Convert.ToString(gp[syntax]);
        if (name == "GPUNAME")
        {
            NameGPU.Text = valuegpu;
        }
        else if (name == "Availability")
        {
            AvailGPU.Text = "Status code: " + valuegpu;
        }
        else if (name == "CONFMEC")
        {
            CMEC.Text = "Error code: " + valuegpu;
        }
        else if (name == "DVer")
        {
            DriverVers.Text = "Driver Version: " + valuegpu;
        }
        else if (name == "ERDESC")
        {
            ErrDescr.Text = valuegpu;
        }
        else if (name == "ERRCLR")
        {
            ErrorClear.Text = valuegpu;
        }
        else if (name == "STAT")
        {
            StatusGPU.Text = "Status: " + valuegpu;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

一个简单的更改将WMI查询过滤到所请求的字段,使我的查询加速了100倍以上:

ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", $"SELECT {syntax} FROM " + hwclass);

但是,它仍然无缘无故地进行多个查询。可以编写一种更高效的版本,可以一次性查询所有需要的字段,并且仍然快3倍。此版本包括其他建议的改进:

private void cpuinfos(List<string> dataElements) {
    ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", $"SELECT {String.Join(",", dataElements)} FROM Win32_Processor");
    foreach (ManagementObject cpuInfo in mos.Get()) {
        foreach (var anElementName in dataElements) {
            string valuebro = Convert.ToString(cpuInfo[anElementName]);
            switch (anElementName) {
                case "Name":
                    CPUNAME.Text = valuebro;
                    break;
                case "MaxClockSpeed":
                    //CPUMAXFREQ.Text = "Clocked at " + valuebro + "Mhz";
                    maxfreqbro = Convert.ToInt32(valuebro);
                    break;
                case "CurrentClockSpeed":
                    curfreqbro = Convert.ToInt32(valuebro);
                    CPUMAXFREQ.Text = "Frequency: " + valuebro + "Mhz";
                    break;
                case "ThreadCount":
                    THREADC.Text = "Threads: " + valuebro;
                    break;
                case "DataWidth":
                    DataW.Text = "Data Width: " + valuebro + " bit";
                    break;
                case "L2CacheSize":
                    l2size = Convert.ToInt32(valuebro) / 1000;
                    L2Size.Text = "L2 Size:  " + l2size.ToString() + " MB";
                    break;
                case "L3CacheSize":
                    l3size = Convert.ToInt32(valuebro) / 1000;
                    L3Size.Text = "L3 Size:  " + l3size.ToString() + " MB";
                    break;
                case "NumberOfCores":
                    NoOfCores.Text = "Cores: " + valuebro;
                    break;
                case "CurrentVoltage":
                    voltage = Convert.ToDecimal(valuebro) / 10.0m;
                    output = voltage.ToString();
                    CurrentVolt.Text = "Core Voltage: " + output + " V";
                    break;
                case "Status":
                    CPUSTATUS.Text = "Status: " + valuebro;
                    break;
                case "Availability":
                    Avail.Text = "Code: " + valuebro;
                    break;
            }
        }
    }
}

通过传递要收集的值来使用它:

cpuinfos(new List<string>() { "Name", "MaxClockSpeed", "ThreadCount" });