为什么让PerformanceCounter代码崩溃

时间:2019-07-01 08:22:19

标签: c# wpf cpu-usage

这段代码使我的WPF程序崩溃

    void DoButton(object sender, RoutedEventArgs e)
    {

        int i = 0;
        int x = 0;
        PerformanceCounter cpuCounter;
        cpuCounter = new PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";
        while (modus == 0)
        {
            float currentCpuUsage = cpuCounter.NextValue();
            TextOutput.Text = "" + currentCpuUsage;
            Thread.Sleep(300);
        }

    }

没有错误消息,但整个窗口崩溃了

0 个答案:

没有答案
相关问题