vs2010 获取64位操作系统CPU使用率

2025-10-27 00:11:46

1、创建项目后,在 Visual Studio 工具栏上,打开“解决方案平台”下拉列表框,如下图所示:

vs2010 获取64位操作系统CPU使用率

2、单击“配置管理器”,在“配置管理器”对话框中,打开“活动解决方案平台”下拉列表框并单击“<新建> …”,如下图所示:

vs2010 获取64位操作系统CPU使用率

3、在“新建解决方案平台”对话框的“键入或选择新平台”下拉列表框中,选择“x64”,如下图所示:

vs2010 获取64位操作系统CPU使用率

4、在“从此处复制设置”下拉列表框中选择“x86”,单击“确定”,如下图所示:

vs2010 获取64位操作系统CPU使用率

5、在“配置管理器”对话框中,确保“生成”列中与解决方案内的所有项目对应的框均处于选中状态,单击“关闭”,如下图所示:

vs2010 获取64位操作系统CPU使用率

1、统计全部CPU的使用状况,最简单的方法就是使用Windows的任务管理器程序。

但默认是只显示“非空闲”执行时间的百分比。在Windows 7任务管理器视图菜单里有“显示内核时间”这一项,这样的话,用户时间和内核时间就区分开了。内核时间是红色部分,如下图所示:

vs2010 获取64位操作系统CPU使用率

2、在编程上,使用性能计数器可以进行更详细的CPU时间监控。比如下面自己做一个CPU使用监控的程序,新建一个WPF程序,创建完的目录结构如下图所示:

vs2010 获取64位操作系统CPU使用率

3、创建一个Item.cs类,代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ComponentModel;

namespace WpfApplication1

{

    class Item : INotifyPropertyChanged

    {

        public Item(string name, string counter)

        {

            DisplayName = name;

            CounterName = counter;

        }

        public string DisplayName { get; private set; }

        public string CounterName { get; private set; }

        private double _Value;

        public double Value

        {

            get { return _Value; }

            set

            {

                if (value != _Value)

                {

                    _Value = value;

                    OnPropertyChanged("Value");

                }

            }

        }

        protected virtual void OnPropertyChanged(string propname)

        {

            if (PropertyChanged != null)

                PropertyChanged(this, new PropertyChangedEventArgs(propname));

        }

        public event PropertyChangedEventHandler PropertyChanged;

    }

}

vs2010 获取64位操作系统CPU使用率

4、创建一个MainWindow.xaml文件,代码如下图所示:

<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="暖枫无敌2015的电脑CPU使用监控" Height="350

    <ItemsControl Name="list">

        <ItemsControl.ItemTemplate>

            <DataTemplate>

                <Border Margin="5" Padding="5">

                    <StackPanel>

                        <TextBlock Text="{Binding DisplayName}"/>

                        <ProgressBar Height="20"

                                     Value="{Binding Value}"/>

                        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" >

                            <TextBlock Text="{Binding Value}" FontWeight="Bold"/>

                            <TextBlock Text="%"/>

                        </StackPanel>

                    </StackPanel>

                </Border>

            </DataTemplate>

        </ItemsControl.ItemTemplate>

    </ItemsControl>

</Window>

vs2010 获取64位操作系统CPU使用率

5、MainWindow.xaml.cs文件,代码如下图所示:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Threading;

using System.Diagnostics;

using System.IO;

using System.Management;

namespace WpfApplication1

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        Item idle = new Item("空闲", "Idle");

        Item interrupt = new Item("硬件中断", "Interrupt");

        Item user = new Item("用户时间", "User");

        Item kernel = new Item("内核时间", "Privileged");

        Item nonIdle = new Item("工作时间", "Processor");

        Item[] items;

        PerformanceCounter[] counters;

        public MainWindow()

        {

            InitializeComponent();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            items = new Item[] { idle, interrupt, user, kernel, nonIdle };

            list.ItemsSource = items;

            counters = new PerformanceCounter[items.Length];

            for (int i = 0; i < counters.Length; i++)

            {

                counters[i] = new PerformanceCounter("Processor", string.Format("% {0} Time", items[i].CounterName), "_Total");

            }

            Refresh();

        }

        void Refresh()

        {

            new Thread(() =>

                {

                    while (true)

                    {

                        for (int i = 0; i < counters.Length; i++)

                        {

                            var index = i;

                            var value = Math.Round(counters[i].NextValue(), 4);

                            Dispatcher.BeginInvoke((Action)(() =>

                            {

                                items[index].Value = value;

                            }));

                        }

                        Thread.Sleep(1000);

                    }

                }) { IsBackground = true }.Start();

        }

    }

}

vs2010 获取64位操作系统CPU使用率

6、编译生成并运行WPF程序,如下图所示:

vs2010 获取64位操作系统CPU使用率

7、以上就是利用VS2010开发的获取64位操作系统CPU使用率的步骤。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢