c# - Why does using WMI to get CPU usage freeze my program for a few seconds? -


i use code usage value of 4 cores in cpu, , display each 1 own label:

            string[] cpuusage = new string[100];             int = 0;              //get cpu usage values using wmi             managementobjectsearcher searcher = new managementobjectsearcher("select * win32_perfformatteddata_perfos_processor");             foreach (managementobject obj in searcher.get())             {                 cpuusage[i] = obj["percentprocessortime"].tostring();                 i++;             }              la_core1.content = "core usage: " + cpuusage[0] + "%";             la_core2.content = "core usage: " + cpuusage[1] + "%";             la_core3.content = "core usage: " + cpuusage[2] + "%";             la_core4.content = "core usage: " + cpuusage[3] + "%"; 

however, first time code run, freezes program around 10 seconds before displaying value. past first time, however, code runs near instantly. happening here make slow on first run, not on subsequent run?

when use wmi query on computer, execute query , not until returns results. prevent this, utilize multithreading can run query without locking program else.

as time execute query, happens me sometimes, can assume local machine caches results degree. so, subsequent searches quicker. might notice if close program , re-test operations time, cache flushed first query take long time again.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -