Тема: Узнаем загруженность процессора
Класс Analytics позволяет узнавать общую нагрузку на CPU, нагрузку созданную текущим Silverlight приложением, а также получить информацию о GPU.
Следующий код сообщает информацию о GPU и текущую нагрузку на процессор с интервалом в 1 сек.:
XAML:
<UserControl x:Class="CpuTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Средняя загруженность процессора: " />
<TextBlock x:Name="tbAveProcessorLoad" Text="NaN" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Средняя нагрузка процесса Silverlight: " />
<TextBlock x:Name="tbAveProcessLoad" Text="NaN" />
</StackPanel>
<StackPanel Margin="10">
<TextBlock Text="GPU: " />
<TextBlock x:Name="tbGpuCollection" Text="" />
</StackPanel>
</StackPanel>
</Grid>
</UserControl>using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace CpuTest
{
public partial class MainPage : UserControl
{
DispatcherTimer timer = new DispatcherTimer();
Analytics analytics = new Analytics();
public MainPage()
{
InitializeComponent();
// определяем все GPU:
foreach (GpuInformation gpuInfo in analytics.GpuCollection)
{
tbGpuCollection.Text
+= " Device Id: " + gpuInfo.DeviceId
+ "\n Driver Version: " + gpuInfo.DriverVersion
+ "\n Vendor Id: " + gpuInfo.VendorId + "\n\n";
}
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += (sender, e) =>
{
tbAveProcessorLoad.Text = analytics.AverageProcessorLoad.ToString();
tbAveProcessLoad.Text = analytics.AverageProcessLoad.ToString();
};
timer.Start();
}
}
}Демонстрация примера: