我認爲這很簡單,但我很困惑如何設置變量「result」(請參閱下面的代碼),以便稍後在程序中調用它,計時器顯示當前內存使用量除以安裝的內存總量以收集使用率百分比。 WMI收集已安裝的RAM的方式一直在拋棄我,因爲它必須執行結果[「TotalVisibleMemorySize」]。在定時器中存放整塊代碼的問題是,由於WMI速度較慢,因此它每2秒刷新一次,這會嚴重滯後計數器。謝謝!在調用一個變量但不通過程序時出現問題c#
private void Form1_Load(object sender, EventArgs e)
{
ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();
foreach (ManagementObject result in results)
{
label1.Text = Convert.ToInt32(result["TotalVisibleMemorySize"]) + " KB";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Value = (int)(performanceCounter1.NextValue() - Convert.ToInt32(result["TotalVisibleMemorySize"]));
label1.Text = "Processor Time: " + progressBar1.Value.ToString() + "%";
}