Im currently messing around with a timer and a label and a performance counter because I am trying to make a label update its content to what ever the CPU usage is at the moment (and keeps updating at a certain interval)
But for some reason it just shows that my CPU usage is at 0% and its not updating.
What is causing this bug.
public void timerControl()
{
DispatcherTimer dtClockTime = new DispatcherTimer();
dtClockTime.Interval = new TimeSpan(0, 0, 1); //in Hour, Minutes, Second.
dtClockTime.Tick += dtClockTime_Tick;
dtClockTime.IsEnabled = true;
dtClockTime.Start();
}
private void dtClockTime_Tick(object sender, EventArgs e)
{
getCPUInfo();
}
public void getCPUInfo()
{
PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
// Get Current Cpu Usage
string currentCpuUsage =
cpuCounter.NextValue() + "%";
//Print it to the current label
CPUUsage.Content = currentCpuUsage;
}
XAML
<Page x:Class="Hawk_PC.systemPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Hawk_PC"
mc:Ignorable="d"
Background="#03a3d2"
d:DesignHeight="350" d:DesignWidth="525"
Title="systemPage">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Label x:Name="osInfoLabel" Content="osInfoFriendlyName" HorizontalAlignment="Left" Margin="-206,-76,0,0" VerticalAlignment="Top"/>
<Image x:Name="image" HorizontalAlignment="Left" Height="83" Margin="-258,94,0,-119" VerticalAlignment="Top" Width="100" Source="Images/Logo.png"/>
<Label x:Name="osInfo" Content="OS:" HorizontalAlignment="Left" Margin="-234,-76,0,0" VerticalAlignment="Top"/>
<Label x:Name="ramInfo" Content="RAM:" HorizontalAlignment="Left" Margin="-234,-50,0,0" VerticalAlignment="Top"/>
<Label x:Name="hddInfo" Content="HDD Storage:" HorizontalAlignment="Left" Margin="-234,2,0,0" VerticalAlignment="Top"/>
<Label x:Name="systemHealth" Content="System Diagnose:" HorizontalAlignment="Left" Margin="-234,28,0,0" VerticalAlignment="Top"/>
<Label x:Name="cpuInfo" Content="CPU:" HorizontalAlignment="Left" Margin="-234,-24,0,0" VerticalAlignment="Top"/>
<Label x:Name="CPUUsage" Content="Label" HorizontalAlignment="Left" Margin="-190,-24,0,0" VerticalAlignment="Top"/>
<Button x:Name="startScanButton" Click="startScanButton_Click" Background="Transparent" BorderThickness="0" Content="Start Diagnose" HorizontalAlignment="Left" Margin="-258,55,0,-10" VerticalAlignment="Top" Width="137" Height="31"/>
</Grid>
</Page>