C#后台工作者标签不更新

时间:2015-01-21 04:51:33

标签: label backgroundworker

                        if (process.ProcessName == procName)
                        {
                            // Prime the Performance Counters
                            pcProcess.NextValue();
                            cpuUsage.NextValue();
                            Thread.Sleep(100);

                            double cpuUse = Math.Round(pcProcess.NextValue() / cpuUsage.NextValue() * 100, 2);

                            if (Double.IsNaN(cpuUse))
                                cpuUse = 0;

                            System.DateTime newDate = System.DateTime.Now;

                            double memUseage = process.PrivateMemorySize64 / 1048576;
                            string memusg = memUseage.ToString();
                            label13.Invoke(new Action(() => { label13.Text = "RAM: " + memusg + "mb"; }));

                            //Get CPU Usage
                            Int32 usg = Convert.ToInt32(cpuUsage);
                            label12.Invoke(new Action(() => { label12.Text = "CPU: " + usg.ToString() + "%"; }));

                            Thread.Sleep(100);
                        }

一切正常,但CPU使用标签不会更新。我做了一些检查,他停在代码

Int32 usg = Convert.ToInt32(cpuUsage);

有人能解释一下这里发生了什么吗?

编辑: 我也可以在这里添加这段代码

using (PerformanceCounter cpuUsage = new PerformanceCounter("Process", "% Processor Time", "_Total"))

0 个答案:

没有答案