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

 时间:2024-11-13 11:11:21

本篇经验将和大家介绍VS2010 获取64位操作系统CPU使用率的方法,希望对大家的工作和学习有所帮助!

Visual Studio 2010 启用 x64 作为 CPU 平台目标

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

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

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

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

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

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

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

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

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

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

VS2010中WPF代码实现

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" Width="525" Loaded="Window_Loaded"> <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使用率的步骤。

  • Excel VBA-----之do while loop的用法
  • odbc连接aspen实时数据库ip21,错误193
  • C# 文件、文件夹重命名 引用程序集 VisualBasic
  • SQLyog的简单使用
  • SQL Server新快捷键如何配置为表设计器
  • 热门搜索
    关于春天的手抄报花边 好看的手抄报边框 争做文明少年手抄报 生态文明手抄报内容 低碳环保我先行手抄报 我爱读书的手抄报 初中交通安全手抄报 有关于感恩的手抄报 中秋节手抄报的内容 八十天环游地球手抄报